Zend_Http_Response represents an HTTP 1.0 / 1.1 response message. It includes easy access to all the response's different elemts, as well as some convenience methods for parsing and validating HTTP responses.

package Zend_Http
subpackage Response
copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
license New BSD License

 Methods

HTTP response constructor

__construct(integer $code, array $headers, string $body = null, string $version = '1.1', string $message = null

In most cases, you would use Zend_Http_Response::fromString to parse an HTTP response string and create a new Zend_Http_Response object.

NOTE: The constructor no longer accepts nulls or empty values for the code and headers and will throw an exception if the passed values do not form a valid HTTP responses.

If no message is passed, the message will be guessed according to the response code.

Parameters

$code

integer

Response code (200, 404, ...)

$headers

array

Headers array

$body

string

Response body

$version

string

HTTP version

$message

string

Response code as text

Exceptions

\Zend_Http_Exception

Implements magic __toString()

__toString() : string

Returns

string

Get the entire response as string

asString(string $br = "\r\n") : string

Parameters

$br

string

Line breaks (eg. "\n", "\r\n", "
")

Returns

string

Decode a "chunked" transfer-encoded body and return the decoded text

decodeChunkedBody(string $body) : string
Static

Parameters

$body

string

Returns

string

Decode a zlib deflated message (when Content-encoding = deflate)

decodeDeflate(string $body) : string
Static

Currently requires PHP with zlib support

Parameters

$body

string

Returns

string

Decode a gzip encoded message (when Content-encoding = gzip)

decodeGzip(string $body) : string
Static

Currently requires PHP with zlib support

Parameters

$body

string

Returns

string

Extract the body from a response string

extractBody(string $response_str) : string
Static

Parameters

$response_str

string

Returns

string

Extract the response code from a response string

extractCode(string $response_str) : integer
Static

Parameters

$response_str

string

Returns

integer

Extract the headers from a response string

extractHeaders(string $response_str) : array
Static

Parameters

$response_str

string

Returns

array

Extract the HTTP message from a response

extractMessage(string $response_str) : string
Static

Parameters

$response_str

string

Returns

string

Extract the HTTP version from a response

extractVersion(string $response_str) : string
Static

Parameters

$response_str

string

Returns

string

Create a new Zend_Http_Response object from a string

fromString(string $response_str) : \Zend_Http_Response
Static

Parameters

$response_str

string

Returns

\Zend_Http_Response

Get the response body as string

getBody() : string

This method returns the body of the HTTP response (the content), as it should be in it's readable version - that is, after decoding it (if it was decoded), deflating it (if it was gzip compressed), etc.

If you want to get the raw body (as transfered on wire) use $this->getRawBody() instead.

Returns

string

Get a specific header as string, or null if it is not set

getHeader($header) : string | array | null

Parameters

$header

Returns

stringarraynull

Get the response headers

getHeaders() : array

Returns

array

Get all headers as string

getHeadersAsString(boolean $status_line = true, string $br = "\n") : string

Parameters

$status_line

boolean

Whether to return the first status line (IE "HTTP 200 OK")

$br

string

Line breaks (eg. "\n", "\r\n", "
")

Returns

string

Return a message describing the HTTP response code (Eg. "OK", "Not Found", "Moved Permanently")

getMessage() : string

Returns

string

Get the raw response body (as transfered "on wire") as string

getRawBody() : string

If the body is encoded (with Transfer-Encoding, not content-encoding - IE "chunked" body), gzip compressed, etc. it will not be decoded.

Returns

string

Get the HTTP response status code

getStatus() : integer

Returns

integer

Get the HTTP version of the response

getVersion() : string

Returns

string

Check whether the response is an error

isError() : boolean

Returns

boolean

Check whether the response is a redirection

isRedirect() : boolean

Returns

boolean

Check whether the response in successful

isSuccessful() : boolean

Returns

boolean

A convenience function that returns a text representation of HTTP response codes. Returns 'Unknown' for unknown codes.

responseCodeAsText(integer $code = null, boolean $http11 = true) : string
Static

Returns array of all codes, if $code is not specified.

Conforms to HTTP/1.1 as defined in RFC 2616 (except for 'Unknown') See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10 for reference

Parameters

$code

integer

HTTP response code

$http11

boolean

Use HTTP version 1.1

Returns

string

 Properties

 

The HTTP response body

$body : string

Default

 

The HTTP response code

$code : integer

Default

 

The HTTP response headers array

$headers : array

Default

array()
 

The HTTP response code as string (e.g. 'Not Found' for 404 or 'Internal Server Error' for 500)

$message : string

Default

 

List of all known HTTP response codes - used by responseCodeAsText() to translate numeric codes to messages.

$messages : array

Default

array(100 => 'Continue', 101 => 'Switching Protocols', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 509 => 'Bandwidth Limit Exceeded')
Static
 

The HTTP version (1.0, 1.1)

$version : string

Default