Zend_Http_Client-Adapters.xml 30 KB

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