2
0

Zend_Http_Client.xml 11 KB

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