Zend_Http_Client.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. <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
  55. these 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>
  87. Whether to strictly follow the <acronym>RFC</acronym> when
  88. redirecting (see <xref linkend="zend.http.client.redirections" />)
  89. </entry>
  90. <entry>boolean</entry>
  91. <entry><constant>FALSE</constant></entry>
  92. </row>
  93. <row>
  94. <entry>useragent</entry>
  95. <entry>User agent identifier string (sent in request headers)</entry>
  96. <entry>string</entry>
  97. <entry>'Zend_Http_Client'</entry>
  98. </row>
  99. <row>
  100. <entry>timeout</entry>
  101. <entry>Connection timeout (seconds)</entry>
  102. <entry>integer</entry>
  103. <entry>10</entry>
  104. </row>
  105. <row>
  106. <entry>httpversion</entry>
  107. <entry>HTTP protocol version (usually '1.1' or '1.0')</entry>
  108. <entry>string</entry>
  109. <entry>'1.1'</entry>
  110. </row>
  111. <row>
  112. <entry>adapter</entry>
  113. <entry>
  114. Connection adapter class to use (see <xref
  115. linkend="zend.http.client.adapters" />)
  116. </entry>
  117. <entry>mixed</entry>
  118. <entry>'Zend_Http_Client_Adapter_Socket'</entry>
  119. </row>
  120. <row>
  121. <entry>keepalive</entry>
  122. <entry>
  123. Whether to enable keep-alive connections with the server. Useful and
  124. might improve performance if several consecutive requests to the
  125. same server are performed.
  126. </entry>
  127. <entry>boolean</entry>
  128. <entry><constant>FALSE</constant></entry>
  129. </row>
  130. <row>
  131. <entry>storeresponse</entry>
  132. <entry>
  133. Whether to store last response for later retrieval with
  134. <methodname>getLastResponse()</methodname>. If set to
  135. <constant>FALSE</constant>
  136. <methodname>getLastResponse()</methodname> will return
  137. <constant>NULL</constant>.
  138. </entry>
  139. <entry>boolean</entry>
  140. <entry><constant>TRUE</constant></entry>
  141. </row>
  142. </tbody>
  143. </tgroup>
  144. </table>
  145. </para>
  146. </sect2>
  147. <sect2 id="zend.http.client.basic-requests">
  148. <title>Performing Basic HTTP Requests</title>
  149. <para>
  150. Performing simple HTTP requests is very easily done using the
  151. request() method, and rarely needs more than three lines of code:
  152. <example id="zend.http.client.basic-requests.example-1">
  153. <title>Performing a Simple GET Request</title>
  154. <programlisting language="php"><![CDATA[
  155. $client = new Zend_Http_Client('http://example.org');
  156. $response = $client->request();
  157. ]]></programlisting>
  158. </example>
  159. The request() method takes one optional parameter - the request method.
  160. This can be either GET, POST, PUT, HEAD, DELETE, TRACE, OPTIONS or
  161. CONNECT as defined by the HTTP protocol
  162. <footnote>
  163. <para>
  164. See RFC 2616 - <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616.html" />.
  165. </para>
  166. </footnote>.
  167. For convenience, these are all defined as class constants:
  168. Zend_Http_Client::GET, Zend_Http_Client::POST and so on.
  169. </para>
  170. <para>
  171. If no method is specified, the method set by the last setMethod()
  172. call is used. If setMethod() was never called, the default request
  173. method is GET (see the above example).
  174. <example id="zend.http.client.basic-requests.example-2">
  175. <title>Using Request Methods Other Than GET</title>
  176. <programlisting language="php"><![CDATA[
  177. // Preforming a POST request
  178. $response = $client->request('POST');
  179. // Yet another way of preforming a POST request
  180. $client->setMethod(Zend_Http_Client::POST);
  181. $response = $client->request();
  182. ]]></programlisting>
  183. </example>
  184. </para>
  185. </sect2>
  186. <sect2 id="zend.http.client.parameters">
  187. <title>Adding GET and POST parameters </title>
  188. <para>
  189. Adding GET parameters to an HTTP request is quite simple, and can
  190. be done either by specifying them as part of the URL, or by using
  191. the setParameterGet() method.
  192. This method takes the GET parameter's name as its first parameter,
  193. and the GET parameter's value as its second parameter.
  194. For convenience, the setParameterGet() method can also accept a
  195. single associative array of name => value GET variables - which may
  196. be more comfortable when several GET parameters need to be set.
  197. <example id="zend.http.client.parameters.example-1">
  198. <title>Setting GET Parameters</title>
  199. <programlisting language="php"><![CDATA[
  200. // Setting a get parameter using the setParameterGet method
  201. $client->setParameterGet('knight', 'lancelot');
  202. // This is equivalent to setting such URL:
  203. $client->setUri('http://example.com/index.php?knight=lancelot');
  204. // Adding several parameters with one call
  205. $client->setParameterGet(array(
  206. 'first_name' => 'Bender',
  207. 'middle_name' => 'Bending'
  208. 'made_in' => 'Mexico',
  209. ));
  210. ]]></programlisting>
  211. </example>
  212. </para>
  213. <para>
  214. While GET parameters can be sent with every request method, POST
  215. parameters are only sent in the body of POST requests. Adding POST
  216. parameters to a request is very similar to adding GET parameters,
  217. and can be done with the setParameterPost() method, which is
  218. similar to the setParameterGet() method in structure.
  219. <example id="zend.http.client.parameters.example-2">
  220. <title>Setting POST Parameters</title>
  221. <programlisting language="php"><![CDATA[
  222. // Setting a POST parameter
  223. $client->setParameterPost('language', 'fr');
  224. // Setting several POST parameters, one of them with several values
  225. $client->setParameterPost(array(
  226. 'language' => 'es',
  227. 'country' => 'ar',
  228. 'selection' => array(45, 32, 80)
  229. ));
  230. ]]></programlisting>
  231. </example>
  232. Note that when sending POST requests, you can set both GET and
  233. POST parameters. On the other hand, while setting POST parameters
  234. for a non-POST request will not trigger and error, it is useless.
  235. Unless the request is a POST request, POST parameters are simply
  236. ignored.
  237. </para>
  238. </sect2>
  239. <sect2 id="zend.http.client.accessing_last">
  240. <title>Accessing Last Request and Response</title>
  241. <para>
  242. <classname>Zend_Http_Client</classname> provides methods of accessing the last request
  243. sent and last response received by the client object.
  244. <classname>Zend_Http_Client->getLastRequest()</classname> takes no parameters
  245. and returns the last <acronym>HTTP</acronym> request sent by the client as a string.
  246. Similarly, <classname>Zend_Http_Client->getLastResponse()</classname> returns
  247. the last <acronym>HTTP</acronym> response received by the client as a
  248. <link linkend="zend.http.response">Zend_Http_Response</link> object.
  249. </para>
  250. </sect2>
  251. </sect1>
  252. <!--
  253. vim:se ts=4 sw=4 et:
  254. -->