Zend_Http_Response.xml 13 KB

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