Zend_Http_Client-Adapters.xml 30 KB

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