Zend_Http_Response.xml 14 KB

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