Zend_Http_Response.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. <code>public function __construct($code, $headers, $body = null, $version = '1.1',
  41. $message = null)</code>
  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. <code>Boolean isSuccessful()</code>: Whether the request was successful or
  86. not. Returns <constant>TRUE</constant> for <acronym>HTTP</acronym> 1xx and
  87. 2xx response codes
  88. </para>
  89. </listitem>
  90. <listitem>
  91. <para>
  92. <code>Boolean isError()</code>: Whether the response code implies an error
  93. or not. Returns <constant>TRUE</constant> for <acronym>HTTP</acronym> 4xx
  94. (client errors) and 5xx (server errors) response codes
  95. </para>
  96. </listitem>
  97. <listitem>
  98. <para>
  99. <code>Boolean isRedirect()</code>: 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. <code>int getStatus()</code>: Get the <acronym>HTTP</acronym> response
  127. status code (eg. 200, 504, etc.)
  128. </para>
  129. </listitem>
  130. <listitem>
  131. <para>
  132. <code>string getMessage()</code>: Get the <acronym>HTTP</acronym> response
  133. status message (eg. "Not Found", "Authorization Required")
  134. </para>
  135. </listitem>
  136. <listitem>
  137. <para>
  138. <code>string getBody()</code>: Get the fully decoded <acronym>HTTP</acronym>
  139. response body
  140. </para>
  141. </listitem>
  142. <listitem>
  143. <para>
  144. <code>string getRawBody()</code>: 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. <code>array getHeaders()</code>: Get the <acronym>HTTP</acronym> response
  152. headers as an associative array (eg. 'Content-type' => 'text/html')
  153. </para>
  154. </listitem>
  155. <listitem>
  156. <para>
  157. <code>string|array getHeader($header)</code>: Get a specific
  158. <acronym>HTTP</acronym> response header, specified by $header
  159. </para>
  160. </listitem>
  161. <listitem>
  162. <para>
  163. <code>string getHeadersAsString($status_line = true, $br = "\n")</code>: Get
  164. the entire set of headers as a string. If $status_line 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 $br parameter (Can
  167. be, for example, "&lt;br /&gt;")
  168. </para>
  169. </listitem>
  170. <listitem>
  171. <para>
  172. <code>string asString($br = "\n")</code>: Get the entire response message as
  173. a string. Lines are broken with the $br parameter (Can be, for example,
  174. "&lt;br /&gt;"). You can also use the magic method __toString() when casting
  175. the object as a string. It will then proxy to asString()
  176. </para>
  177. </listitem>
  178. </itemizedlist>
  179. <example id="zend.http.response.acessors.example-1">
  180. <title>Using Zend_Http_Response Accessor Methods</title>
  181. <programlisting language="php"><![CDATA[
  182. if ($response->getStatus() == 200) {
  183. echo "The request returned the following information:<br />";
  184. echo $response->getBody();
  185. } else {
  186. echo "An error occurred while fetching data:<br />";
  187. echo $response->getStatus() . ": " . $response->getMessage();
  188. }
  189. ]]></programlisting>
  190. </example>
  191. <note>
  192. <title>Always check return value</title>
  193. <para>
  194. Since a response can contain several instances of the same header,
  195. the getHeader() method and getHeaders() method may return either a
  196. single string, or an array of strings for each header. You should
  197. always check whether the returned value is a string or array.
  198. </para>
  199. </note>
  200. <example id="zend.http.response.acessors.example-2">
  201. <title>Accessing Response Headers</title>
  202. <programlisting language="php"><![CDATA[
  203. $ctype = $response->getHeader('Content-type');
  204. if (is_array($ctype)) $ctype = $ctype[0];
  205. $body = $response->getBody();
  206. if ($ctype == 'text/html' || $ctype == 'text/xml') {
  207. $body = htmlentities($body);
  208. }
  209. echo $body;
  210. ]]></programlisting>
  211. </example>
  212. </para>
  213. </sect2>
  214. <sect2 id="zend.http.response.static_parsers">
  215. <title>Static HTTP Response Parsers</title>
  216. <para>
  217. The <classname>Zend_Http_Response</classname> class also includes several
  218. internally-used methods for processing and parsing <acronym>HTTP</acronym> response
  219. messages. These methods are all exposed as static methods, which means they can be
  220. used externally, even if you do not need to instantiate a response
  221. object, and just want to extract a specific part of the response.
  222. <itemizedlist>
  223. <listitem>
  224. <para>
  225. <code>int Zend_Http_Response::extractCode($response_str)</code>: Extract
  226. and return the <acronym>HTTP</acronym> response code (eg. 200 or 404) from
  227. $response_str
  228. </para>
  229. </listitem>
  230. <listitem>
  231. <para>
  232. <code>string Zend_Http_Response::extractMessage($response_str)</code>:
  233. Extract and return the <acronym>HTTP</acronym> response message (eg. "OK" or
  234. "File Not Found") from $response_str
  235. </para>
  236. </listitem>
  237. <listitem>
  238. <para>
  239. <code>string Zend_Http_Response::extractVersion($response_str)</code>:
  240. Extract and return the <acronym>HTTP</acronym> version (eg. 1.1 or 1.0) from
  241. $response_str
  242. </para>
  243. </listitem>
  244. <listitem>
  245. <para>
  246. <code>array Zend_Http_Response::extractHeaders($response_str)</code>:
  247. Extract and return the <acronym>HTTP</acronym> response headers from
  248. $response_str as an array
  249. </para>
  250. </listitem>
  251. <listitem>
  252. <para>
  253. <code>string Zend_Http_Response::extractBody($response_str)</code>: Extract
  254. and return the <acronym>HTTP</acronym> response body from $response_str
  255. </para>
  256. </listitem>
  257. <listitem>
  258. <para>
  259. <code>string Zend_Http_Response::responseCodeAsText($code = null, $http11 =
  260. true)</code>: Get the standard <acronym>HTTP</acronym> response message for
  261. a response code $code. For example, will return "Internal Server Error" if
  262. $code is 500. If $http11 is <constant>TRUE</constant> (default), will return
  263. <acronym>HTTP</acronym>/1.1 standard messages - otherwise
  264. <acronym>HTTP</acronym>/1.0 messages will be returned. If $code is not
  265. specified, this method will return all known <acronym>HTTP</acronym>
  266. response codes as an associative (code => message) array.
  267. </para>
  268. </listitem>
  269. </itemizedlist>
  270. </para>
  271. <para>
  272. Apart from parser methods, the class also includes a set of decoders for common
  273. <acronym>HTTP</acronym> response transfer encodings:
  274. <itemizedlist>
  275. <listitem>
  276. <para>
  277. <code>string Zend_Http_Response::decodeChunkedBody($body)</code>: Decode
  278. a complete "Content-Transfer-Encoding: Chunked" body
  279. </para>
  280. </listitem>
  281. <listitem>
  282. <para>
  283. <code>string Zend_Http_Response::decodeGzip($body)</code>: Decode
  284. a "Content-Encoding: gzip" body
  285. </para>
  286. </listitem>
  287. <listitem>
  288. <para>
  289. <code>string Zend_Http_Response::decodeDeflate($body)</code>: Decode
  290. a "Content-Encoding: deflate" body
  291. </para>
  292. </listitem>
  293. </itemizedlist>
  294. </para>
  295. </sect2>
  296. </sect1>
  297. <!--
  298. vim:se ts=4 sw=4 et:
  299. -->