Zend_Http_Client.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.http.client">
  4. <title>Introduction</title>
  5. <para>
  6. <classname>Zend_Http_Client</classname> provides an easy interface for preforming Hyper-Text
  7. Transfer Protocol (HTTP) requests. <classname>Zend_Http_Client</classname> supports most simple
  8. features expected from an <acronym>HTTP</acronym> client, as well as some more complex
  9. features such as <acronym>HTTP</acronym> authentication and file uploads. Successful
  10. requests (and most unsuccessful ones too) return a <classname>Zend_Http_Response</classname>
  11. object, which provides access to the response's headers and body (see
  12. <xref linkend="zend.http.response" />).
  13. </para>
  14. <sect2 id="zend.http.client.usage">
  15. <title>Using Zend_Http_Client</title>
  16. <para>
  17. The class constructor optionally accepts a URL as its first parameter
  18. (can be either a string or a Zend_Uri_Http object), and an array or
  19. Zend_Config object containing configuration options. Both can be left out,
  20. and set later using the setUri() and setConfig() methods.
  21. <example id="zend.http.client.introduction.example-1">
  22. <title>Instantiating a Zend_Http_Client Object</title>
  23. <programlisting language="php"><![CDATA[
  24. $client = new Zend_Http_Client('http://example.org', array(
  25. 'maxredirects' => 0,
  26. 'timeout' => 30));
  27. // This is actually exactly the same:
  28. $client = new Zend_Http_Client();
  29. $client->setUri('http://example.org');
  30. $client->setConfig(array(
  31. 'maxredirects' => 0,
  32. 'timeout' => 30));
  33. // You can also use a Zend_Config object to set the client's configuration
  34. $config = new Zend_Config_Ini('httpclient.ini, 'secure');
  35. $client->setConfig($config);
  36. ]]></programlisting>
  37. </example>
  38. <note>
  39. <para>
  40. Zend_Http_Client uses Zend_Uri_Http to validate URLs. This means
  41. that some special characters like the pipe symbol ('|') or the
  42. caret symbol ('^') will not be accepted in the URL by default.
  43. This can be modified by setting the 'allow_unwise' option of
  44. Zend_Uri to '<constant>TRUE</constant>'. See <xref
  45. linkend="zend.uri.validation.allowunwise" /> for more information.
  46. </para>
  47. </note>
  48. </para>
  49. </sect2>
  50. <sect2 id="zend.http.client.configuration">
  51. <title>Configuration Parameters</title>
  52. <para>
  53. The constructor and setConfig() method accept an associative array
  54. of configuration parameters, or a <classname>Zend_Config</classname> object. Setting these
  55. parameters is optional, as they all have default values.
  56. <table id="zend.http.client.configuration.table">
  57. <title>Zend_Http_Client configuration parameters</title>
  58. <tgroup cols="4">
  59. <thead>
  60. <row>
  61. <entry>Parameter</entry>
  62. <entry>Description</entry>
  63. <entry>Expected Values</entry>
  64. <entry>Default Value</entry>
  65. </row>
  66. </thead>
  67. <tbody>
  68. <row>
  69. <entry>maxredirects</entry>
  70. <entry>Maximum number of redirections to follow (0 = none)</entry>
  71. <entry>integer</entry>
  72. <entry>5</entry>
  73. </row>
  74. <row>
  75. <entry>strict</entry>
  76. <entry>
  77. Whether perform validation on header names. When set to
  78. <constant>FALSE</constant>, validation functions will be skipped.
  79. Usually this should not be changed
  80. </entry>
  81. <entry>boolean</entry>
  82. <entry><constant>TRUE</constant></entry>
  83. </row>
  84. <row>
  85. <entry>strictredirects</entry>
  86. <entry>Whether to strictly follow the <acronym>RFC</acronym> when redirecting (see <xref linkend="zend.http.client.redirections" />)</entry>
  87. <entry>boolean</entry>
  88. <entry><constant>FALSE</constant></entry>
  89. </row>
  90. <row>
  91. <entry>useragent</entry>
  92. <entry>User agent identifier string (sent in request headers)</entry>
  93. <entry>string</entry>
  94. <entry>'Zend_Http_Client'</entry>
  95. </row>
  96. <row>
  97. <entry>timeout</entry>
  98. <entry>Connection timeout (seconds)</entry>
  99. <entry>integer</entry>
  100. <entry>10</entry>
  101. </row>
  102. <row>
  103. <entry>httpversion</entry>
  104. <entry>HTTP protocol version (usually '1.1' or '1.0')</entry>
  105. <entry>string</entry>
  106. <entry>'1.1'</entry>
  107. </row>
  108. <row>
  109. <entry>adapter</entry>
  110. <entry>Connection adapter class to use (see <xref linkend="zend.http.client.adapters" />)</entry>
  111. <entry>mixed</entry>
  112. <entry>'Zend_Http_Client_Adapter_Socket'</entry>
  113. </row>
  114. <row>
  115. <entry>keepalive</entry>
  116. <entry>Whether to enable keep-alive connections with the server. Useful and might improve performance if several
  117. consecutive requests to the same server are performed.</entry>
  118. <entry>boolean</entry>
  119. <entry><constant>FALSE</constant></entry>
  120. </row>
  121. <row>
  122. <entry>storeresponse</entry>
  123. <entry>
  124. Whether to store last response for later retrieval with
  125. <methodname>getLastResponse()</methodname>. If set to
  126. <constant>FALSE</constant>
  127. <methodname>getLastResponse()</methodname> will return
  128. <constant>NULL</constant>.
  129. </entry>
  130. <entry>boolean</entry>
  131. <entry><constant>TRUE</constant></entry>
  132. </row>
  133. </tbody>
  134. </tgroup>
  135. </table>
  136. </para>
  137. </sect2>
  138. <sect2 id="zend.http.client.basic-requests">
  139. <title>Performing Basic HTTP Requests</title>
  140. <para>
  141. Performing simple HTTP requests is very easily done using the
  142. request() method, and rarely needs more than three lines of code:
  143. <example id="zend.http.client.basic-requests.example-1">
  144. <title>Performing a Simple GET Request</title>
  145. <programlisting language="php"><![CDATA[
  146. $client = new Zend_Http_Client('http://example.org');
  147. $response = $client->request();
  148. ]]></programlisting>
  149. </example>
  150. The request() method takes one optional parameter - the request method.
  151. This can be either GET, POST, PUT, HEAD, DELETE, TRACE, OPTIONS or
  152. CONNECT as defined by the HTTP protocol
  153. <footnote>
  154. <para>
  155. See RFC 2616 - <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616.html" />.
  156. </para>
  157. </footnote>.
  158. For convenience, these are all defined as class constants:
  159. Zend_Http_Client::GET, Zend_Http_Client::POST and so on.
  160. </para>
  161. <para>
  162. If no method is specified, the method set by the last setMethod()
  163. call is used. If setMethod() was never called, the default request
  164. method is GET (see the above example).
  165. <example id="zend.http.client.basic-requests.example-2">
  166. <title>Using Request Methods Other Than GET</title>
  167. <programlisting language="php"><![CDATA[
  168. // Preforming a POST request
  169. $response = $client->request('POST');
  170. // Yet another way of preforming a POST request
  171. $client->setMethod(Zend_Http_Client::POST);
  172. $response = $client->request();
  173. ]]></programlisting>
  174. </example>
  175. </para>
  176. </sect2>
  177. <sect2 id="zend.http.client.parameters">
  178. <title>Adding GET and POST parameters </title>
  179. <para>
  180. Adding GET parameters to an HTTP request is quite simple, and can
  181. be done either by specifying them as part of the URL, or by using
  182. the setParameterGet() method.
  183. This method takes the GET parameter's name as its first parameter,
  184. and the GET parameter's value as its second parameter.
  185. For convenience, the setParameterGet() method can also accept a
  186. single associative array of name => value GET variables - which may
  187. be more comfortable when several GET parameters need to be set.
  188. <example id="zend.http.client.parameters.example-1">
  189. <title>Setting GET Parameters</title>
  190. <programlisting language="php"><![CDATA[
  191. // Setting a get parameter using the setParameterGet method
  192. $client->setParameterGet('knight', 'lancelot');
  193. // This is equivalent to setting such URL:
  194. $client->setUri('http://example.com/index.php?knight=lancelot');
  195. // Adding several parameters with one call
  196. $client->setParameterGet(array(
  197. 'first_name' => 'Bender',
  198. 'middle_name' => 'Bending'
  199. 'made_in' => 'Mexico',
  200. ));
  201. ]]></programlisting>
  202. </example>
  203. </para>
  204. <para>
  205. While GET parameters can be sent with every request method, POST
  206. parameters are only sent in the body of POST requests. Adding POST
  207. parameters to a request is very similar to adding GET parameters,
  208. and can be done with the setParameterPost() method, which is
  209. similar to the setParameterGet() method in structure.
  210. <example id="zend.http.client.parameters.example-2">
  211. <title>Setting POST Parameters</title>
  212. <programlisting language="php"><![CDATA[
  213. // Setting a POST parameter
  214. $client->setParameterPost('language', 'fr');
  215. // Setting several POST parameters, one of them with several values
  216. $client->setParameterPost(array(
  217. 'language' => 'es',
  218. 'country' => 'ar',
  219. 'selection' => array(45, 32, 80)
  220. ));
  221. ]]></programlisting>
  222. </example>
  223. Note that when sending POST requests, you can set both GET and
  224. POST parameters. On the other hand, while setting POST parameters
  225. for a non-POST request will not trigger and error, it is useless.
  226. Unless the request is a POST request, POST parameters are simply
  227. ignored.
  228. </para>
  229. </sect2>
  230. <sect2 id="zend.http.client.accessing_last">
  231. <title>Accessing Last Request and Response</title>
  232. <para>
  233. <classname>Zend_Http_Client</classname> provides methods of accessing the last request
  234. sent and last response received by the client object.
  235. <classname>Zend_Http_Client->getLastRequest()</classname> takes no parameters
  236. and returns the last <acronym>HTTP</acronym> request sent by the client as a string.
  237. Similarly, <classname>Zend_Http_Client->getLastResponse()</classname> returns
  238. the last <acronym>HTTP</acronym> response received by the client as a
  239. <link linkend="zend.http.response">Zend_Http_Response</link> object.
  240. </para>
  241. </sect2>
  242. </sect1>
  243. <!--
  244. vim:se ts=4 sw=4 et:
  245. -->