Zend_Http_Response.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 role="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 role="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. </para>
  163. </listitem>
  164. </itemizedlist>
  165. <example id="zend.http.response.acessors.example-1">
  166. <title>Using Zend_Http_Response Accessor Methods</title>
  167. <programlisting role="php"><![CDATA[
  168. if ($response->getStatus() == 200) {
  169. echo "The request returned the following information:<br />";
  170. echo $response->getBody();
  171. } else {
  172. echo "An error occurred while fetching data:<br />";
  173. echo $response->getStatus() . ": " . $response->getMessage();
  174. }
  175. ]]></programlisting>
  176. </example>
  177. <note>
  178. <title>Always check return value</title>
  179. <para>
  180. Since a response can contain several instances of the same header,
  181. the getHeader() method and getHeaders() method may return either a
  182. single string, or an array of strings for each header. You should
  183. always check whether the returned value is a string or array.
  184. </para>
  185. </note>
  186. <example id="zend.http.response.acessors.example-2">
  187. <title>Accessing Response Headers</title>
  188. <programlisting role="php"><![CDATA[
  189. $ctype = $response->getHeader('Content-type');
  190. if (is_array($ctype)) $ctype = $ctype[0];
  191. $body = $response->getBody();
  192. if ($ctype == 'text/html' || $ctype == 'text/xml') {
  193. $body = htmlentities($body);
  194. }
  195. echo $body;
  196. ]]></programlisting>
  197. </example>
  198. </para>
  199. </sect2>
  200. <sect2 id="zend.http.response.static_parsers">
  201. <title>Static HTTP Response Parsers</title>
  202. <para>
  203. The Zend_Http_Response class also includes several internally-used
  204. methods for processing and parsing HTTP response messages. These
  205. methods are all exposed as static methods, which means they can be
  206. used externally, even if you do not need to instantiate a response
  207. object, and just want to extract a specific part of the response.
  208. <itemizedlist>
  209. <listitem>
  210. <para>
  211. <code>int Zend_Http_Response::extractCode($response_str)</code>: Extract
  212. and return the HTTP response code (eg. 200 or 404) from $response_str
  213. </para>
  214. </listitem>
  215. <listitem>
  216. <para>
  217. <code>string Zend_Http_Response::extractMessage($response_str)</code>: Extract
  218. and return the HTTP response message (eg. "OK" or "File Not Found") from $response_str
  219. </para>
  220. </listitem>
  221. <listitem>
  222. <para>
  223. <code>string Zend_Http_Response::extractVersion($response_str)</code>: : Extract
  224. and return the HTTP version (eg. 1.1 or 1.0) from $response_str
  225. </para>
  226. </listitem>
  227. <listitem>
  228. <para>
  229. <code>array Zend_Http_Response::extractHeaders($response_str)</code>: Extract
  230. and return the HTTP response headers from $response_str as an array
  231. </para>
  232. </listitem>
  233. <listitem>
  234. <para>
  235. <code>string Zend_Http_Response::extractBody($response_str)</code>: Extract
  236. and return the HTTP response body from $response_str
  237. </para>
  238. </listitem>
  239. <listitem>
  240. <para>
  241. <code>string Zend_Http_Response::responseCodeAsText($code = null, $http11 = true)</code>:
  242. Get the standard HTTP response message for a response code $code. For example,
  243. will return "Internal Server Error" if $code is 500. If $http11 is true (default),
  244. will return HTTP/1.1 standard messages - otherwise HTTP/1.0 messages will be returned.
  245. If $code is not specified, this method will return all known HTTP response codes as an
  246. associative (code => message) array.
  247. </para>
  248. </listitem>
  249. </itemizedlist>
  250. </para>
  251. <para>
  252. Apart from parser methods, the class also includes a set of decoders for common HTTP
  253. response transfer encodings:
  254. <itemizedlist>
  255. <listitem>
  256. <para>
  257. <code>string Zend_Http_Response::decodeChunkedBody($body)</code>: Decode
  258. a complete "Content-Transfer-Encoding: Chunked" body
  259. </para>
  260. </listitem>
  261. <listitem>
  262. <para>
  263. <code>string Zend_Http_Response::decodeGzip($body)</code>: Decode
  264. a "Content-Encoding: gzip" body
  265. </para>
  266. </listitem>
  267. <listitem>
  268. <para>
  269. <code>string Zend_Http_Response::decodeDeflate($body)</code>: Decode
  270. a "Content-Encoding: deflate" body
  271. </para>
  272. </listitem>
  273. </itemizedlist>
  274. </para>
  275. </sect2>
  276. </sect1>
  277. <!--
  278. vim:se ts=4 sw=4 et:
  279. -->