Zend_Http_Client.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. <row>
  143. <entry>encodecookies</entry>
  144. <entry>
  145. Whether to pass the cookie value through urlencode/urldecode.
  146. Enabling this breaks support with some web servers.
  147. Disabling this limits the range of values the cookies can contain.
  148. </entry>
  149. <entry>boolean</entry>
  150. <entry><constant>TRUE</constant></entry>
  151. </row>
  152. </tbody>
  153. </tgroup>
  154. </table>
  155. </para>
  156. </sect2>
  157. <sect2 id="zend.http.client.basic-requests">
  158. <title>Performing Basic HTTP Requests</title>
  159. <para>
  160. Performing simple HTTP requests is very easily done using the
  161. request() method, and rarely needs more than three lines of code:
  162. <example id="zend.http.client.basic-requests.example-1">
  163. <title>Performing a Simple GET Request</title>
  164. <programlisting language="php"><![CDATA[
  165. $client = new Zend_Http_Client('http://example.org');
  166. $response = $client->request();
  167. ]]></programlisting>
  168. </example>
  169. The request() method takes one optional parameter - the request method.
  170. This can be either GET, POST, PUT, HEAD, DELETE, TRACE, OPTIONS or
  171. CONNECT as defined by the HTTP protocol
  172. <footnote>
  173. <para>
  174. See RFC 2616 - <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616.html" />.
  175. </para>
  176. </footnote>.
  177. For convenience, these are all defined as class constants:
  178. Zend_Http_Client::GET, Zend_Http_Client::POST and so on.
  179. </para>
  180. <para>
  181. If no method is specified, the method set by the last setMethod()
  182. call is used. If setMethod() was never called, the default request
  183. method is GET (see the above example).
  184. <example id="zend.http.client.basic-requests.example-2">
  185. <title>Using Request Methods Other Than GET</title>
  186. <programlisting language="php"><![CDATA[
  187. // Preforming a POST request
  188. $response = $client->request('POST');
  189. // Yet another way of preforming a POST request
  190. $client->setMethod(Zend_Http_Client::POST);
  191. $response = $client->request();
  192. ]]></programlisting>
  193. </example>
  194. </para>
  195. </sect2>
  196. <sect2 id="zend.http.client.parameters">
  197. <title>Adding GET and POST parameters </title>
  198. <para>
  199. Adding GET parameters to an HTTP request is quite simple, and can
  200. be done either by specifying them as part of the URL, or by using
  201. the setParameterGet() method.
  202. This method takes the GET parameter's name as its first parameter,
  203. and the GET parameter's value as its second parameter.
  204. For convenience, the setParameterGet() method can also accept a
  205. single associative array of name => value GET variables - which may
  206. be more comfortable when several GET parameters need to be set.
  207. <example id="zend.http.client.parameters.example-1">
  208. <title>Setting GET Parameters</title>
  209. <programlisting language="php"><![CDATA[
  210. // Setting a get parameter using the setParameterGet method
  211. $client->setParameterGet('knight', 'lancelot');
  212. // This is equivalent to setting such URL:
  213. $client->setUri('http://example.com/index.php?knight=lancelot');
  214. // Adding several parameters with one call
  215. $client->setParameterGet(array(
  216. 'first_name' => 'Bender',
  217. 'middle_name' => 'Bending'
  218. 'made_in' => 'Mexico',
  219. ));
  220. ]]></programlisting>
  221. </example>
  222. </para>
  223. <para>
  224. While GET parameters can be sent with every request method, POST
  225. parameters are only sent in the body of POST requests. Adding POST
  226. parameters to a request is very similar to adding GET parameters,
  227. and can be done with the setParameterPost() method, which is
  228. similar to the setParameterGet() method in structure.
  229. <example id="zend.http.client.parameters.example-2">
  230. <title>Setting POST Parameters</title>
  231. <programlisting language="php"><![CDATA[
  232. // Setting a POST parameter
  233. $client->setParameterPost('language', 'fr');
  234. // Setting several POST parameters, one of them with several values
  235. $client->setParameterPost(array(
  236. 'language' => 'es',
  237. 'country' => 'ar',
  238. 'selection' => array(45, 32, 80)
  239. ));
  240. ]]></programlisting>
  241. </example>
  242. Note that when sending POST requests, you can set both GET and
  243. POST parameters. On the other hand, while setting POST parameters
  244. for a non-POST request will not trigger and error, it is useless.
  245. Unless the request is a POST request, POST parameters are simply
  246. ignored.
  247. </para>
  248. </sect2>
  249. <sect2 id="zend.http.client.accessing_last">
  250. <title>Accessing Last Request and Response</title>
  251. <para>
  252. <classname>Zend_Http_Client</classname> provides methods of accessing the last request
  253. sent and last response received by the client object.
  254. <classname>Zend_Http_Client->getLastRequest()</classname> takes no parameters
  255. and returns the last <acronym>HTTP</acronym> request sent by the client as a string.
  256. Similarly, <classname>Zend_Http_Client->getLastResponse()</classname> returns
  257. the last <acronym>HTTP</acronym> response received by the client as a
  258. <link linkend="zend.http.response">Zend_Http_Response</link> object.
  259. </para>
  260. </sect2>
  261. </sect1>
  262. <!--
  263. vim:se ts=4 sw=4 et:
  264. -->