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