Zend_Http_Response.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.http.response">
  4. <title>Zend_Http_Response</title>
  5. <sect2 id="zend.http.response.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Http_Response</classname> provides easy access to an
  9. <acronym>HTTP</acronym> responses message, as well as a set of static methods for
  10. parsing <acronym>HTTP</acronym> response messages. Usually,
  11. <classname>Zend_Http_Response</classname> is used as an object returned by a
  12. <classname>Zend_Http_Client</classname> request.
  13. </para>
  14. <para>
  15. In most cases, a <classname>Zend_Http_Response</classname> object will be instantiated
  16. using the fromString() method, which reads a string containing a
  17. <acronym>HTTP</acronym> response message, and returns a new
  18. <classname>Zend_Http_Response</classname> object:
  19. <example id="zend.http.response.introduction.example-1">
  20. <title>Instantiating a Zend_Http_Response Object Using the Factory Method</title>
  21. <programlisting language="php"><![CDATA[
  22. $str = '';
  23. $sock = fsockopen('www.example.com', 80);
  24. $req = "GET / HTTP/1.1\r\n" .
  25. "Host: www.example.com\r\n" .
  26. "Connection: close\r\n" .
  27. "\r\n";
  28. fwrite($sock, $req);
  29. while ($buff = fread($sock, 1024))
  30. $str .= $sock;
  31. $response = Zend_Http_Response::fromString($str);
  32. ]]></programlisting>
  33. </example>
  34. </para>
  35. <para>
  36. You can also use the contractor method to create a new response
  37. object, by specifying all the parameters of the response:
  38. </para>
  39. <para>
  40. <command>public function __construct($code, $headers, $body = null, $version = '1.1',
  41. $message = null)</command>
  42. </para>
  43. <itemizedlist>
  44. <listitem>
  45. <para>
  46. <varname>$code</varname>: The <acronym>HTTP</acronym> response code (eg. 200,
  47. 404, etc.)
  48. </para>
  49. </listitem>
  50. <listitem>
  51. <para>
  52. <varname>$headers</varname>: An associative array of <acronym>HTTP</acronym>
  53. response headers (eg. 'Host' => 'example.com')
  54. </para>
  55. </listitem>
  56. <listitem>
  57. <para>
  58. <varname>$body</varname>: The response body as a string
  59. </para>
  60. </listitem>
  61. <listitem>
  62. <para>
  63. <varname>$version</varname>: The <acronym>HTTP</acronym> response version
  64. (usually 1.0 or 1.1)
  65. </para>
  66. </listitem>
  67. <listitem>
  68. <para>
  69. <varname>$message</varname>: The <acronym>HTTP</acronym> response message (eg
  70. 'OK', 'Internal Server Error'). If not specified, the message will be set
  71. according to the response code
  72. </para>
  73. </listitem>
  74. </itemizedlist>
  75. </sect2>
  76. <sect2 id="zend.http.response.testers">
  77. <title>Boolean Tester Methods</title>
  78. <para>
  79. Once a <classname>Zend_Http_Response</classname> object is instantiated, it provides
  80. several methods that can be used to test the type of the response. These all
  81. return Boolean <constant>TRUE</constant> or <constant>FALSE</constant>:
  82. <itemizedlist>
  83. <listitem>
  84. <para>
  85. <methodname>isSuccessful()</methodname>: Whether the request was successful
  86. or not. Returns <constant>TRUE</constant> for <acronym>HTTP</acronym> 1xx
  87. and 2xx response codes
  88. </para>
  89. </listitem>
  90. <listitem>
  91. <para>
  92. <methodname>isError()</methodname>: Whether the response code implies an
  93. error or not. Returns <constant>TRUE</constant> for <acronym>HTTP</acronym>
  94. 4xx (client errors) and 5xx (server errors) response codes
  95. </para>
  96. </listitem>
  97. <listitem>
  98. <para>
  99. <methodname>isRedirect()</methodname>: Whether the response is a redirection
  100. response or not. Returns <constant>TRUE</constant> for
  101. <acronym>HTTP</acronym> 3xx response codes
  102. </para>
  103. </listitem>
  104. </itemizedlist>
  105. <example id="zend.http.response.testers.example-1">
  106. <title>Using the isError() method to validate a response</title>
  107. <programlisting language="php"><![CDATA[
  108. if ($response->isError()) {
  109. echo "Error transmitting data.\n"
  110. echo "Server reply was: " . $response->getStatus() .
  111. " " . $response->getMessage() . "\n";
  112. }
  113. // .. process the response here...
  114. ]]></programlisting>
  115. </example>
  116. </para>
  117. </sect2>
  118. <sect2 id="zend.http.response.acessors">
  119. <title>Accessor Methods</title>
  120. <para>
  121. The main goal of the response object is to provide easy access to
  122. various response parameters.
  123. <itemizedlist>
  124. <listitem>
  125. <para>
  126. <methodname>getStatus()</methodname>: Get the <acronym>HTTP</acronym>
  127. response status code (eg. 200, 504, etc.)
  128. </para>
  129. </listitem>
  130. <listitem>
  131. <para>
  132. <methodname>getMessage()</methodname>: Get the <acronym>HTTP</acronym>
  133. response status message (eg. "Not Found", "Authorization Required")
  134. </para>
  135. </listitem>
  136. <listitem>
  137. <para>
  138. <methodname>getBody()</methodname>: Get the fully decoded
  139. <acronym>HTTP</acronym> response body
  140. </para>
  141. </listitem>
  142. <listitem>
  143. <para>
  144. <methodname>getRawBody()</methodname>: Get the raw, possibly encoded
  145. <acronym>HTTP</acronym> response body. if the body was decoded using GZIP
  146. encoding for example, it will not be decoded.
  147. </para>
  148. </listitem>
  149. <listitem>
  150. <para>
  151. <methodname>getHeaders()</methodname>: Get the <acronym>HTTP</acronym>
  152. response headers as an associative array (eg. 'Content-type' => 'text/html')
  153. </para>
  154. </listitem>
  155. <listitem>
  156. <para>
  157. <methodname>getHeader($header)</methodname>: Get a specific
  158. <acronym>HTTP</acronym> response header, specified by $header
  159. </para>
  160. </listitem>
  161. <listitem>
  162. <para>
  163. <methodname>getHeadersAsString($status_line, $br)</methodname>: Get
  164. the entire set of headers as a string. If <varname>$status_line</varname> is
  165. <constant>TRUE</constant> (default), the first status line (eg. "HTTP/1.1
  166. 200 OK") will also be returned. Lines are broken with the
  167. <varname>$br</varname> parameter (Can be, for example, "&lt;br /&gt;".
  168. Default "\n")
  169. </para>
  170. </listitem>
  171. <listitem>
  172. <para>
  173. <methodname>asString($br)</methodname>: Get the entire response message as
  174. a string. Lines are broken with the $br parameter (Can be, for example,
  175. "&lt;br /&gt;". Default "\n"). You can also use the magic method
  176. <methodname>__toString()</methodname> when casting the object as a string.
  177. It will then proxy to <methodname>asString()</methodname>.
  178. </para>
  179. </listitem>
  180. </itemizedlist>
  181. <example id="zend.http.response.acessors.example-1">
  182. <title>Using Zend_Http_Response Accessor Methods</title>
  183. <programlisting language="php"><![CDATA[
  184. if ($response->getStatus() == 200) {
  185. echo "The request returned the following information:<br />";
  186. echo $response->getBody();
  187. } else {
  188. echo "An error occurred while fetching data:<br />";
  189. echo $response->getStatus() . ": " . $response->getMessage();
  190. }
  191. ]]></programlisting>
  192. </example>
  193. <note>
  194. <title>Always check return value</title>
  195. <para>
  196. Since a response can contain several instances of the same header,
  197. the getHeader() method and getHeaders() method may return either a
  198. single string, or an array of strings for each header. You should
  199. always check whether the returned value is a string or array.
  200. </para>
  201. </note>
  202. <example id="zend.http.response.acessors.example-2">
  203. <title>Accessing Response Headers</title>
  204. <programlisting language="php"><![CDATA[
  205. $ctype = $response->getHeader('Content-type');
  206. if (is_array($ctype)) $ctype = $ctype[0];
  207. $body = $response->getBody();
  208. if ($ctype == 'text/html' || $ctype == 'text/xml') {
  209. $body = htmlentities($body);
  210. }
  211. echo $body;
  212. ]]></programlisting>
  213. </example>
  214. </para>
  215. </sect2>
  216. <sect2 id="zend.http.response.static_parsers">
  217. <title>Static HTTP Response Parsers</title>
  218. <para>
  219. The <classname>Zend_Http_Response</classname> class also includes several
  220. internally-used methods for processing and parsing <acronym>HTTP</acronym> response
  221. messages. These methods are all exposed as static methods, which means they can be
  222. used externally, even if you do not need to instantiate a response
  223. object, and just want to extract a specific part of the response.
  224. <itemizedlist>
  225. <listitem>
  226. <para>
  227. <methodname>Zend_Http_Response::extractCode($response_str)</methodname>:
  228. Extract and return the <acronym>HTTP</acronym> response code (eg. 200 or
  229. 404) from <varname>$response_str</varname>
  230. </para>
  231. </listitem>
  232. <listitem>
  233. <para>
  234. <methodname>Zend_Http_Response::extractMessage($response_str)</methodname>:
  235. Extract and return the <acronym>HTTP</acronym> response message (eg. "OK" or
  236. "File Not Found") from <varname>$response_str</varname>
  237. </para>
  238. </listitem>
  239. <listitem>
  240. <para>
  241. <methodname>Zend_Http_Response::extractVersion($response_str)</methodname>:
  242. Extract and return the <acronym>HTTP</acronym> version (eg. 1.1 or 1.0) from
  243. <varname>$response_str</varname>
  244. </para>
  245. </listitem>
  246. <listitem>
  247. <para>
  248. <methodname>Zend_Http_Response::extractHeaders($response_str)</methodname>:
  249. Extract and return the <acronym>HTTP</acronym> response headers from
  250. <varname>$response_str</varname> as an array
  251. </para>
  252. </listitem>
  253. <listitem>
  254. <para>
  255. <methodname>Zend_Http_Response::extractBody($response_str)</methodname>:
  256. Extract and return the <acronym>HTTP</acronym> response body from
  257. <varname>$response_str</varname>
  258. </para>
  259. </listitem>
  260. <listitem>
  261. <para>
  262. <methodname>Zend_Http_Response::responseCodeAsText($code,
  263. $http11)</methodname>: Get the standard <acronym>HTTP</acronym> response
  264. message for a response code $code. For example, will return "Internal Server
  265. Error" if <varname>$code</varname> is 500. If <varname>$http11</varname> is
  266. <constant>TRUE</constant> (default), will return
  267. <acronym>HTTP</acronym>/1.1 standard messages - otherwise
  268. <acronym>HTTP</acronym>/1.0 messages will be returned. If
  269. <varname>$code</varname> is not specified, this method will return all known
  270. <acronym>HTTP</acronym> response codes as an associative (code => message)
  271. array.
  272. </para>
  273. </listitem>
  274. </itemizedlist>
  275. </para>
  276. <para>
  277. Apart from parser methods, the class also includes a set of decoders for common
  278. <acronym>HTTP</acronym> response transfer encodings:
  279. <itemizedlist>
  280. <listitem>
  281. <para>
  282. <methodname>Zend_Http_Response::decodeChunkedBody($body)</methodname>:
  283. Decode a complete "Content-Transfer-Encoding: Chunked" body
  284. </para>
  285. </listitem>
  286. <listitem>
  287. <para>
  288. <methodname>Zend_Http_Response::decodeGzip($body)</methodname>: Decode
  289. a "Content-Encoding: gzip" body
  290. </para>
  291. </listitem>
  292. <listitem>
  293. <para>
  294. <methodname>Zend_Http_Response::decodeDeflate($body)</methodname>: Decode
  295. a "Content-Encoding: deflate" body
  296. </para>
  297. </listitem>
  298. </itemizedlist>
  299. </para>
  300. </sect2>
  301. </sect1>
  302. <!--
  303. vim:se ts=4 sw=4 et:
  304. -->