Zend_Http_Client-Adapters.xml 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.http.client.adapters">
  4. <title>Zend_Http_Client - Connection Adapters</title>
  5. <sect2 id="zend.http.client.adapters.overview">
  6. <title>Overview</title>
  7. <para>
  8. <classname>Zend_Http_Client</classname> is based on a connection adapter design. The
  9. connection adapter is the object in charge of performing the
  10. actual connection to the server, as well as writing requests
  11. and reading responses.
  12. This connection adapter can be replaced, and you can create and
  13. extend the default connection adapters to suite your special needs,
  14. without the need to extend or replace the entire <acronym>HTTP</acronym> client
  15. class, and with the same interface.
  16. </para>
  17. <para>
  18. Currently, the <classname>Zend_Http_Client</classname> class provides four built-in
  19. connection adapters:
  20. <itemizedlist>
  21. <listitem>
  22. <para>
  23. <classname>Zend_Http_Client_Adapter_Socket</classname> (default)
  24. </para>
  25. </listitem>
  26. <listitem>
  27. <para>
  28. <classname>Zend_Http_Client_Adapter_Proxy</classname>
  29. </para>
  30. </listitem>
  31. <listitem>
  32. <para>
  33. <classname>Zend_Http_Client_Adapter_Curl</classname>
  34. </para>
  35. </listitem>
  36. <listitem>
  37. <para>
  38. <classname>Zend_Http_Client_Adapter_Test</classname>
  39. </para>
  40. </listitem>
  41. </itemizedlist>
  42. </para>
  43. <para>
  44. The <classname>Zend_Http_Client</classname> object's adapter connection adapter is set
  45. using the 'adapter' configuration option. When instantiating the
  46. client object, you can set the 'adapter' configuration option to
  47. a string containing the adapter's name (eg. 'Zend_Http_Client_Adapter_Socket')
  48. or to a variable holding an adapter object (eg.
  49. <command>new Zend_Http_Client_Adapter_Test</command>). You can also set the
  50. adapter later, using the <classname>Zend_Http_Client->setConfig()</classname> method.
  51. </para>
  52. </sect2>
  53. <sect2 id="zend.http.client.adapters.socket">
  54. <title>The Socket Adapter</title>
  55. <para>
  56. The default connection adapter is the
  57. <classname>Zend_Http_Client_Adapter_Socket</classname> adapter - this adapter will be
  58. used unless you explicitly set the connection adapter. The Socket adapter is based on
  59. <acronym>PHP</acronym>'s built-in fsockopen() function, and does not require any special
  60. extensions or compilation flags.
  61. </para>
  62. <para>
  63. The Socket adapter allows several extra configuration options that
  64. can be set using <classname>Zend_Http_Client->setConfig()</classname> or
  65. passed to the client constructor.
  66. <table id="zend.http.client.adapter.socket.configuration.table">
  67. <title>Zend_Http_Client_Adapter_Socket configuration parameters</title>
  68. <tgroup cols="4">
  69. <thead>
  70. <row>
  71. <entry>Parameter</entry>
  72. <entry>Description</entry>
  73. <entry>Expected Type</entry>
  74. <entry>Default Value</entry>
  75. </row>
  76. </thead>
  77. <tbody>
  78. <row>
  79. <entry>persistent</entry>
  80. <entry>
  81. Whether to use persistent <acronym>TCP</acronym> connections
  82. </entry>
  83. <entry>boolean</entry>
  84. <entry><constant>FALSE</constant></entry>
  85. </row>
  86. <row>
  87. <entry>ssltransport</entry>
  88. <entry>SSL transport layer (eg. 'sslv2', 'tls')</entry>
  89. <entry>string</entry>
  90. <entry>ssl</entry>
  91. </row>
  92. <row>
  93. <entry>sslcert</entry>
  94. <entry>
  95. Path to a <acronym>PEM</acronym> encoded <acronym>SSL</acronym>
  96. certificate
  97. </entry>
  98. <entry>string</entry>
  99. <entry><constant>NULL</constant></entry>
  100. </row>
  101. <row>
  102. <entry>sslpassphrase</entry>
  103. <entry>
  104. Passphrase for the <acronym>SSL</acronym> certificate file
  105. </entry>
  106. <entry>string</entry>
  107. <entry><constant>NULL</constant></entry>
  108. </row>
  109. <row>
  110. <entry>sslusecontext</entry>
  111. <entry>
  112. Enables proxied connections to use SSL even if
  113. the proxy connection itself does not.
  114. </entry>
  115. <entry>boolean</entry>
  116. <entry><constant>FALSE</constant></entry>
  117. </row>
  118. </tbody>
  119. </tgroup>
  120. </table>
  121. <note>
  122. <title>Persistent TCP Connections</title>
  123. <para>
  124. Using persistent <acronym>TCP</acronym> connections can potentially speed up
  125. <acronym>HTTP</acronym> requests - but in most use cases, will have little
  126. positive effect and might overload the <acronym>HTTP</acronym> server you are
  127. connecting to.
  128. </para>
  129. <para>
  130. It is recommended to use persistent <acronym>TCP</acronym> connections only if
  131. you connect to the same server very frequently, and are
  132. sure that the server is capable of handling a large number
  133. of concurrent connections. In any case you are encouraged
  134. to benchmark the effect of persistent connections on both
  135. the client speed and server load before using this option.
  136. </para>
  137. <para>
  138. Additionally, when using persistent connections it is recommended to enable
  139. Keep-Alive <acronym>HTTP</acronym> requests as described in <link
  140. linkend="zend.http.client.configuration">the configuration section</link> -
  141. otherwise persistent connections might have little or no effect.
  142. </para>
  143. </note>
  144. <note>
  145. <title>HTTPS SSL Stream Parameters</title>
  146. <para>
  147. <property>ssltransport</property>, <property>sslcert</property> and
  148. <property>sslpassphrase</property> are only relevant when connecting using
  149. <acronym>HTTPS</acronym>.
  150. </para>
  151. <para>
  152. While the default <acronym>SSL</acronym> settings should work for most
  153. applications, you might need to change them if the server
  154. you are connecting to requires special client setup. If so,
  155. you should read the sections about <acronym>SSL</acronym> transport layers and
  156. options <ulink
  157. url="http://www.php.net/manual/en/transports.php#transports.inet">here</ulink>.
  158. </para>
  159. </note>
  160. </para>
  161. <example id="zend.http.client.adapters.socket.example-1">
  162. <title>Changing the HTTPS transport layer</title>
  163. <programlisting language="php"><![CDATA[
  164. // Set the configuration parameters
  165. $config = array(
  166. 'adapter' => 'Zend_Http_Client_Adapter_Socket',
  167. 'ssltransport' => 'tls'
  168. );
  169. // Instantiate a client object
  170. $client = new Zend_Http_Client('https://www.example.com', $config);
  171. // The following request will be sent over a TLS secure connection.
  172. $response = $client->request();
  173. ]]></programlisting>
  174. </example>
  175. <para>
  176. The result of the example above will be similar to opening a <acronym>TCP</acronym>
  177. connection using the following <acronym>PHP</acronym> command:
  178. </para>
  179. <para>
  180. <methodname>fsockopen('tls://www.example.com', 443)</methodname>
  181. </para>
  182. <sect3 id="zend.http.client.adapters.socket.streamcontext">
  183. <title>Customizing and accessing the Socket adapter stream context</title>
  184. <para>
  185. Starting from Zend Framework 1.9,
  186. <classname>Zend_Http_Client_Adapter_Socket</classname> provides direct access to the
  187. underlying <ulink
  188. url="http://php.net/manual/en/stream.contexts.php">stream context</ulink> used
  189. to connect to the remote server. This allows the user to pass specific options and
  190. parameters to the <acronym>TCP</acronym> stream, and to the <acronym>SSL</acronym>
  191. wrapper in case of <acronym>HTTPS</acronym> connections.
  192. </para>
  193. <para>
  194. You can access the stream context using the following methods of
  195. <classname>Zend_Http_Client_Adapter_Socket</classname>:
  196. <itemizedlist>
  197. <listitem>
  198. <para>
  199. <firstterm><methodname>setStreamContext($context)</methodname></firstterm>
  200. Sets the stream context to be used by the adapter. Can accept either
  201. a stream context resource created using the <ulink
  202. url="http://php.net/manual/en/function.stream-context-create.php"><methodname>stream_context_create()</methodname></ulink>
  203. <acronym>PHP</acronym> function, or an array of stream context options,
  204. in the same format provided to this function. Providing an array will
  205. create a new stream context using these options, and set it.
  206. </para>
  207. </listitem>
  208. <listitem>
  209. <para>
  210. <firstterm><methodname>getStreamContext()</methodname></firstterm>
  211. Get the stream context of the adapter. If no stream context was set,
  212. will create a default stream context and return it. You can then set
  213. or get the value of different context options using regular
  214. <acronym>PHP</acronym> stream context functions.
  215. </para>
  216. </listitem>
  217. </itemizedlist>
  218. </para>
  219. <example id="zend.http.client.adapters.socket.streamcontext.example-1">
  220. <title>Setting stream context options for the Socket adapter</title>
  221. <programlisting language="php"><![CDATA[
  222. // Array of options
  223. $options = array(
  224. 'socket' => array(
  225. // Bind local socket side to a specific interface
  226. 'bindto' => '10.1.2.3:50505'
  227. ),
  228. 'ssl' => array(
  229. // Verify server side certificate,
  230. // do not accept invalid or self-signed SSL certificates
  231. 'verify_peer' => true,
  232. 'allow_self_signed' => false,
  233. // Capture the peer's certificate
  234. 'capture_peer_cert' => true
  235. )
  236. );
  237. // Create an adapter object and attach it to the HTTP client
  238. $adapter = new Zend_Http_Client_Adapter_Socket();
  239. $client = new Zend_Http_Client();
  240. $client->setAdapter($adapter);
  241. // Method 1: pass the options array to setStreamContext()
  242. $adapter->setStreamContext($options);
  243. // Method 2: create a stream context and pass it to setStreamContext()
  244. $context = stream_context_create($options);
  245. $adapter->setStreamContext($context);
  246. // Method 3: get the default stream context and set the options on it
  247. $context = $adapter->getStreamContext();
  248. stream_context_set_option($context, $options);
  249. // Now, preform the request
  250. $response = $client->request();
  251. // If everything went well, you can now access the context again
  252. $opts = stream_context_get_options($adapter->getStreamContext());
  253. echo $opts['ssl']['peer_certificate'];
  254. ]]></programlisting>
  255. </example>
  256. <note>
  257. <para>
  258. Note that you must set any stream context options before using the adapter
  259. to preform actual requests. If no context is set before preforming
  260. <acronym>HTTP</acronym> requests with the Socket adapter, a default stream
  261. context will be created. This context resource could be accessed after
  262. preforming any requests using the <methodname>getStreamContext()</methodname>
  263. method.
  264. </para>
  265. </note>
  266. </sect3>
  267. </sect2>
  268. <sect2 id="zend.http.client.adapters.proxy">
  269. <title>The Proxy Adapter</title>
  270. <para>
  271. The <classname>Zend_Http_Client_Adapter_Proxy</classname> adapter is similar to the
  272. default Socket adapter - only the connection is made through an <acronym>HTTP</acronym>
  273. proxy server instead of a direct connection to the target server. This
  274. allows usage of <classname>Zend_Http_Client</classname> behind proxy servers - which is
  275. sometimes needed for security or performance reasons.
  276. </para>
  277. <para>
  278. Using the Proxy adapter requires several additional configuration
  279. parameters to be set, in addition to the default 'adapter' option:
  280. <table id="zend.http.client.adapters.proxy.table">
  281. <title>Zend_Http_Client configuration parameters</title>
  282. <tgroup cols="4">
  283. <thead>
  284. <row>
  285. <entry>Parameter</entry>
  286. <entry>Description</entry>
  287. <entry>Expected Type</entry>
  288. <entry>Example Value</entry>
  289. </row>
  290. </thead>
  291. <tbody>
  292. <row>
  293. <entry>proxy_host</entry>
  294. <entry>Proxy server address</entry>
  295. <entry>string</entry>
  296. <entry>'proxy.myhost.com' or '10.1.2.3'</entry>
  297. </row>
  298. <row>
  299. <entry>proxy_port</entry>
  300. <entry>Proxy server <acronym>TCP</acronym> port</entry>
  301. <entry>integer</entry>
  302. <entry>8080 (default) or 81</entry>
  303. </row>
  304. <row>
  305. <entry>proxy_user</entry>
  306. <entry>Proxy user name, if required</entry>
  307. <entry>string</entry>
  308. <entry>'shahar' or '' for none (default)</entry>
  309. </row>
  310. <row>
  311. <entry>proxy_pass</entry>
  312. <entry>Proxy password, if required</entry>
  313. <entry>string</entry>
  314. <entry>'secret' or '' for none (default)</entry>
  315. </row>
  316. <row>
  317. <entry>proxy_auth</entry>
  318. <entry>Proxy <acronym>HTTP</acronym> authentication type</entry>
  319. <entry>string</entry>
  320. <entry>Zend_Http_Client::AUTH_BASIC (default)</entry>
  321. </row>
  322. </tbody>
  323. </tgroup>
  324. </table>
  325. </para>
  326. <para>
  327. proxy_host should always be set - if it is not set, the client will
  328. fall back to a direct connection using
  329. <classname>Zend_Http_Client_Adapter_Socket</classname>. proxy_port defaults to '8080' -
  330. if your proxy listens on a different port you must set this one as well.
  331. </para>
  332. <para>
  333. proxy_user and proxy_pass are only required if your proxy server
  334. requires you to authenticate. Providing these will add a 'Proxy-Authentication'
  335. header to the request. If your proxy does not require authentication,
  336. you can leave these two options out.
  337. </para>
  338. <para>
  339. proxy_auth sets the proxy authentication type, if your proxy server
  340. requires authentication. Possibly values are similar to the ones
  341. accepted by the Zend_Http_Client::setAuth() method. Currently, only
  342. basic authentication (Zend_Http_Client::AUTH_BASIC) is supported.
  343. </para>
  344. <example id="zend.http.client.adapters.proxy.example-1">
  345. <title>Using Zend_Http_Client behind a proxy server</title>
  346. <programlisting language="php"><![CDATA[
  347. // Set the configuration parameters
  348. $config = array(
  349. 'adapter' => 'Zend_Http_Client_Adapter_Proxy',
  350. 'proxy_host' => 'proxy.int.zend.com',
  351. 'proxy_port' => 8000,
  352. 'proxy_user' => 'shahar.e',
  353. 'proxy_pass' => 'bananashaped'
  354. );
  355. // Instantiate a client object
  356. $client = new Zend_Http_Client('http://www.example.com', $config);
  357. // Continue working...
  358. ]]></programlisting>
  359. </example>
  360. <para>
  361. As mentioned, if proxy_host is not set or is set to a blank string,
  362. the connection will fall back to a regular direct connection. This
  363. allows you to easily write your application in a way that allows a
  364. proxy to be used optionally, according to a configuration parameter.
  365. </para>
  366. <note>
  367. <para>
  368. Since the proxy adapter inherits from
  369. <classname>Zend_Http_Client_Adapter_Socket</classname>, you can use the stream
  370. context access method (see <link
  371. linkend="zend.http.client.adapters.socket.streamcontext">this section</link>)
  372. to set stream context options on Proxy connections as demonstrated above.
  373. </para>
  374. </note>
  375. </sect2>
  376. <sect2 id="zend.http.client.adapters.curl">
  377. <title>The cURL Adapter</title>
  378. <para>
  379. cURL is a standard <acronym>HTTP</acronym> client library that is distributed with many
  380. operating systems and can be used in <acronym>PHP</acronym> via the cURL extension. It
  381. offers functionality for many special cases which can occur for a
  382. <acronym>HTTP</acronym> client and make it a perfect choice for a
  383. <acronym>HTTP</acronym> adapter. It supports secure connections, proxy, all sorts of
  384. authentication mechanisms and shines in applications that move large files around
  385. between servers.
  386. </para>
  387. <example id="zend.http.client.adapters.curl.example-1">
  388. <title>Setting cURL options</title>
  389. <programlisting language="php"><![CDATA[
  390. $config = array(
  391. 'adapter' => 'Zend_Http_Client_Adapter_Curl',
  392. 'curloptions' => array(CURLOPT_FOLLOWLOCATION => true),
  393. );
  394. $client = new Zend_Http_Client($uri, $config);
  395. ]]></programlisting>
  396. </example>
  397. <para>
  398. By default the cURL adapter is configured to behave exactly like
  399. the Socket Adapter and it also accepts the same configuration parameters
  400. as the Socket and Proxy adapters. You can also change the cURL options by either
  401. specifying the 'curloptions' key in the constructor of the adapter or by calling
  402. <methodname>setCurlOption($name, $value)</methodname>. The <varname>$name</varname> key
  403. corresponds to the CURL_* constants of the cURL extension. You can
  404. get access to the Curl handle by calling <command>$adapter->getHandle();</command>
  405. </para>
  406. <example id="zend.http.client.adapters.curl.example-2">
  407. <title>Transfering Files by Handle</title>
  408. <para>
  409. You can use cURL to transfer very large files over <acronym>HTTP</acronym> by
  410. filehandle.
  411. </para>
  412. <programlisting language="php"><![CDATA[
  413. $putFileSize = filesize("filepath");
  414. $putFileHandle = fopen("filepath", "r");
  415. $adapter = new Zend_Http_Client_Adapter_Curl();
  416. $client = new Zend_Http_Client();
  417. $client->setAdapter($adapter);
  418. $adapter->setConfig(array(
  419. 'curloptions' => array(
  420. CURLOPT_INFILE => $putFileHandle,
  421. CURLOPT_INFILESIZE => $putFileSize
  422. )
  423. ));
  424. $client->request("PUT");
  425. ]]></programlisting>
  426. </example>
  427. </sect2>
  428. <sect2 id="zend.http.client.adapters.test">
  429. <title>The Test Adapter</title>
  430. <para>
  431. Sometimes, it is very hard to test code that relies on <acronym>HTTP</acronym>
  432. connections. For example, testing an application that pulls an <acronym>RSS</acronym>
  433. feed from a remote server will require a network connection, which is not always
  434. available.
  435. </para>
  436. <para>
  437. For this reason, the <classname>Zend_Http_Client_Adapter_Test</classname> adapter is
  438. provided. You can write your application to use <classname>Zend_Http_Client</classname>,
  439. and just for testing purposes, for example in your unit testing
  440. suite, you can replace the default adapter with a Test adapter (a
  441. mock object), allowing you to run tests without actually
  442. performing server connections.
  443. </para>
  444. <para>
  445. The <classname>Zend_Http_Client_Adapter_Test</classname> adapter provides an additional
  446. method, setResponse() method. This method takes one parameter,
  447. which represents an <acronym>HTTP</acronym> response as either text or a
  448. <classname>Zend_Http_Response</classname> object. Once set, your Test adapter will
  449. always return this response, without even performing an actual <acronym>HTTP</acronym>
  450. request.
  451. </para>
  452. <example id="zend.http.client.adapters.test.example-1">
  453. <title>Testing Against a Single HTTP Response Stub</title>
  454. <programlisting language="php"><![CDATA[
  455. // Instantiate a new adapter and client
  456. $adapter = new Zend_Http_Client_Adapter_Test();
  457. $client = new Zend_Http_Client('http://www.example.com', array(
  458. 'adapter' => $adapter
  459. ));
  460. // Set the expected response
  461. $adapter->setResponse(
  462. "HTTP/1.1 200 OK" . "\r\n" .
  463. "Content-type: text/xml" . "\r\n" .
  464. "\r\n" .
  465. '<?xml version="1.0" encoding="UTF-8"?>' .
  466. '<rss version="2.0" ' .
  467. ' xmlns:content="http://purl.org/rss/1.0/modules/content/"' .
  468. ' xmlns:wfw="http://wellformedweb.org/CommentAPI/"' .
  469. ' xmlns:dc="http://purl.org/dc/elements/1.1/">' .
  470. ' <channel>' .
  471. ' <title>Premature Optimization</title>' .
  472. // and so on...
  473. '</rss>');
  474. $response = $client->request('GET');
  475. // .. continue parsing $response..
  476. ]]></programlisting>
  477. </example>
  478. <para>
  479. The above example shows how you can preset your <acronym>HTTP</acronym> client to
  480. return the response you need. Then, you can continue testing your
  481. own code, without being dependent on a network connection, the server's
  482. response, etc. In this case, the test would continue to check how
  483. the application parses the <acronym>XML</acronym> in the response body.
  484. </para>
  485. <para>
  486. Sometimes, a single method call to an object can result in that
  487. object performing multiple <acronym>HTTP</acronym> transactions. In this case, it's
  488. not possible to use setResponse() alone because there's no
  489. opportunity to set the next response(s) your program might need
  490. before returning to the caller.
  491. </para>
  492. <example id="zend.http.client.adapters.test.example-2">
  493. <title>Testing Against Multiple HTTP Response Stubs</title>
  494. <programlisting language="php"><![CDATA[
  495. // Instantiate a new adapter and client
  496. $adapter = new Zend_Http_Client_Adapter_Test();
  497. $client = new Zend_Http_Client('http://www.example.com', array(
  498. 'adapter' => $adapter
  499. ));
  500. // Set the first expected response
  501. $adapter->setResponse(
  502. "HTTP/1.1 302 Found" . "\r\n" .
  503. "Location: /" . "\r\n" .
  504. "Content-Type: text/html" . "\r\n" .
  505. "\r\n" .
  506. '<html>' .
  507. ' <head><title>Moved</title></head>' .
  508. ' <body><p>This page has moved.</p></body>' .
  509. '</html>');
  510. // Set the next successive response
  511. $adapter->addResponse(
  512. "HTTP/1.1 200 OK" . "\r\n" .
  513. "Content-Type: text/html" . "\r\n" .
  514. "\r\n" .
  515. '<html>' .
  516. ' <head><title>My Pet Store Home Page</title></head>' .
  517. ' <body><p>...</p></body>' .
  518. '</html>');
  519. // inject the http client object ($client) into your object
  520. // being tested and then test your object's behavior below
  521. ]]></programlisting>
  522. </example>
  523. <para>
  524. The setResponse() method clears any responses in the
  525. <classname>Zend_Http_Client_Adapter_Test</classname>'s buffer and sets the
  526. first response that will be returned. The addResponse()
  527. method will add successive responses.
  528. </para>
  529. <para>
  530. The responses will be replayed in the order that they
  531. were added. If more requests are made than the number
  532. of responses stored, the responses will cycle again
  533. in order.
  534. </para>
  535. <para>
  536. In the example above, the adapter is configured to test your
  537. object's behavior when it encounters a 302 redirect. Depending on
  538. your application, following a redirect may or may not be desired
  539. behavior. In our example, we expect that the redirect will be
  540. followed and we configure the test adapter to help us test this.
  541. The initial 302 response is set up with the setResponse() method
  542. and the 200 response to be returned next is added with the
  543. addResponse() method. After configuring the test adapter, inject
  544. the <acronym>HTTP</acronym> client containing the adapter into your object under test
  545. and test its behavior.
  546. </para>
  547. <para>
  548. If you need the adapter to fail on demand you can use
  549. <methodname>setNextRequestWillFail($flag)</methodname>. The method will cause the next
  550. call to <methodname>connect()</methodname> to throw an
  551. <classname>Zend_Http_Client_Adapter_Exception</classname> exception. This can be useful
  552. when your application caches content from an external site (in case the site goes down)
  553. and you want to test this feature.
  554. </para>
  555. <example id="zend.http.client.adapters.test.example-3">
  556. <title>Forcing the adapter to fail</title>
  557. <programlisting language="php"><![CDATA[
  558. // Instantiate a new adapter and client
  559. $adapter = new Zend_Http_Client_Adapter_Test();
  560. $client = new Zend_Http_Client('http://www.example.com', array(
  561. 'adapter' => $adapter
  562. ));
  563. // Force the next request to fail with an exception
  564. $adapter->setNextRequestWillFail(true);
  565. try {
  566. // This call will result in a Zend_Http_Client_Adapter_Exception
  567. $client->request();
  568. } catch (Zend_Http_Client_Adapter_Exception $e) {
  569. // ...
  570. }
  571. // Further requests will work as expected until
  572. // you call setNextRequestWillFail(true) again
  573. ]]></programlisting>
  574. </example>
  575. </sect2>
  576. <sect2 id="zend.http.client.adapters.extending">
  577. <title>Creating your own connection adapters</title>
  578. <para>
  579. You can create your own connection adapters and use them. You could, for
  580. example, create a connection adapter that uses persistent sockets,
  581. or a connection adapter with caching abilities, and use them as
  582. needed in your application.
  583. </para>
  584. <para>
  585. In order to do so, you must create your own adapter class that implements
  586. the <classname>Zend_Http_Client_Adapter_Interface</classname> interface. The following
  587. example shows the skeleton of a user-implemented adapter class. All the public
  588. functions defined in this example must be defined in your adapter as well:
  589. </para>
  590. <example id="zend.http.client.adapters.extending.example-1">
  591. <title>Creating your own connection adapter</title>
  592. <programlisting language="php"><![CDATA[
  593. class MyApp_Http_Client_Adapter_BananaProtocol
  594. implements Zend_Http_Client_Adapter_Interface
  595. {
  596. /**
  597. * Set the configuration array for the adapter
  598. *
  599. * @param array $config
  600. */
  601. public function setConfig($config = array())
  602. {
  603. // This rarely changes - you should usually copy the
  604. // implementation in Zend_Http_Client_Adapter_Socket.
  605. }
  606. /**
  607. * Connect to the remote server
  608. *
  609. * @param string $host
  610. * @param int $port
  611. * @param boolean $secure
  612. */
  613. public function connect($host, $port = 80, $secure = false)
  614. {
  615. // Set up the connection to the remote server
  616. }
  617. /**
  618. * Send request to the remote server
  619. *
  620. * @param string $method
  621. * @param Zend_Uri_Http $url
  622. * @param string $http_ver
  623. * @param array $headers
  624. * @param string $body
  625. * @return string Request as text
  626. */
  627. public function write($method,
  628. $url,
  629. $http_ver = '1.1',
  630. $headers = array(),
  631. $body = '')
  632. {
  633. // Send request to the remote server.
  634. // This function is expected to return the full request
  635. // (headers and body) as a string
  636. }
  637. /**
  638. * Read response from server
  639. *
  640. * @return string
  641. */
  642. public function read()
  643. {
  644. // Read response from remote server and return it as a string
  645. }
  646. /**
  647. * Close the connection to the server
  648. *
  649. */
  650. public function close()
  651. {
  652. // Close the connection to the remote server - called last.
  653. }
  654. }
  655. // Then, you could use this adapter:
  656. $client = new Zend_Http_Client(array(
  657. 'adapter' => 'MyApp_Http_Client_Adapter_BananaProtocol'
  658. ));
  659. ]]></programlisting>
  660. </example>
  661. </sect2>
  662. </sect1>