Zend_Http_Client-Adapters.xml 31 KB

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