Zend_Http_Response.xml 12 KB

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