Zend_Http_Client.xml 13 KB

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