Zend_Http_Client.xml 14 KB

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