Zend_Http_Client.xml 12 KB

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