\Cake\Http\ClientResponse

Implements methods for HTTP responses.

All of the following examples assume that $response is an instance of this class.

Get header values

Header names are case-insensitive, but normalized to Title-Case when the response is parsed.

$val = $response->getHeaderLine('content-type');

Will read the Content-Type header. You can get all set headers using:

$response->getHeaders();

Get the response body

You can access the response body stream using:

$content = $response->getBody();

You can get the body string using:

$content = $response->getStringBody();

If your response body is in XML or JSON you can use special content type specific accessors to read the decoded data. JSON data will be returned as arrays, while XML data will be returned as SimpleXML nodes:

// Get as xml
$content = $response->getXml()
// Get as json
$content = $response->getJson()

If the response cannot be decoded, null will be returned.

Check the status code

You can access the response status code using:

$content = $response->getStatusCode();

Summary

Methods
Properties
Constants
getProtocolVersion()
withProtocolVersion()
getHeaders()
hasHeader()
getHeader()
getHeaderLine()
withHeader()
withAddedHeader()
withoutHeader()
getBody()
withBody()
headers()
cookies()
body()
__construct()
isOk()
isRedirect()
statusCode()
getStatusCode()
withStatus()
getReasonPhrase()
encoding()
getEncoding()
header()
cookie()
getCookies()
getCookieCollection()
getCookie()
getCookieData()
version()
getStringBody()
getJson()
getXml()
__get()
__isset()
$headers
STATUS_OK
STATUS_CREATED
STATUS_ACCEPTED
STATUS_NON_AUTHORITATIVE_INFORMATION
STATUS_NO_CONTENT
STATUS_MOVED_PERMANENTLY
STATUS_FOUND
STATUS_SEE_OTHER
STATUS_TEMPORARY_REDIRECT
METHOD_GET
METHOD_POST
METHOD_PUT
METHOD_DELETE
METHOD_PATCH
METHOD_OPTIONS
METHOD_TRACE
METHOD_HEAD
_decodeGzipBody()
_parseHeaders()
convertCookieToArray()
buildCookieCollection()
_getCookies()
_getJson()
_getXml()
_getHeaders()
_getBody()
$headers
$headerNames
$_cookies
$_body
$code
$cookies
$reasonPhrase
$_xml
$_json
$_exposedProperties
N/A
getStream()
setHeaders()
validateProtocolVersion()
filterHeaderValue()
assertHeader()
$protocol
$stream
N/A

Constants

STATUS_OK

STATUS_OK = 200 : integer

HTTP 200 code

STATUS_CREATED

STATUS_CREATED = 201 : integer

HTTP 201 code

STATUS_ACCEPTED

STATUS_ACCEPTED = 202 : integer

HTTP 202 code

STATUS_NON_AUTHORITATIVE_INFORMATION

STATUS_NON_AUTHORITATIVE_INFORMATION = 203 : integer

HTTP 203 code

STATUS_NO_CONTENT

STATUS_NO_CONTENT = 204 : integer

HTTP 204 code

STATUS_MOVED_PERMANENTLY

STATUS_MOVED_PERMANENTLY = 301 : integer

HTTP 301 code

STATUS_FOUND

STATUS_FOUND = 302 : integer

HTTP 302 code

STATUS_SEE_OTHER

STATUS_SEE_OTHER = 303 : integer

HTTP 303 code

STATUS_TEMPORARY_REDIRECT

STATUS_TEMPORARY_REDIRECT = 307 : integer

HTTP 307 code

METHOD_GET

METHOD_GET = 'GET' : string

HTTP GET method

METHOD_POST

METHOD_POST = 'POST' : string

HTTP POST method

METHOD_PUT

METHOD_PUT = 'PUT' : string

HTTP PUT method

METHOD_DELETE

METHOD_DELETE = 'DELETE' : string

HTTP DELETE method

METHOD_PATCH

METHOD_PATCH = 'PATCH' : string

HTTP PATCH method

METHOD_OPTIONS

METHOD_OPTIONS = 'OPTIONS' : string

HTTP OPTIONS method

METHOD_TRACE

METHOD_TRACE = 'TRACE' : string

HTTP TRACE method

METHOD_HEAD

METHOD_HEAD = 'HEAD' : string

HTTP HEAD method

Properties

$headers

$headers : array

Type

array

$headers

$headers : array

List of all registered headers, as key => array of values.

Type

array

$headerNames

$headerNames : array

Map of normalized header name to original name used to register header.

Type

array

$_cookies

$_cookies : array

The array of cookies in the response.

Type

array

$_body

$_body : string|null

Body for the message.

Type

string|null

$code

$code : integer

The status code of the response.

Type

integer

$reasonPhrase

$reasonPhrase : string

The reason phrase for the status code

Type

string

$_xml

$_xml : \SimpleXMLElement

Cached decoded XML data.

Type

\SimpleXMLElement

$_json

$_json : array

Cached decoded JSON data.

Type

array

$_exposedProperties

$_exposedProperties : array

Map of public => property names for __get()

Type

array

$protocol

$protocol : string

Type

string

Methods

getProtocolVersion()

getProtocolVersion() : string

Retrieves the HTTP protocol version as a string.

The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").

Returns

string —

HTTP protocol version.

withProtocolVersion()

withProtocolVersion(string  $version) : static

Return an instance with the specified HTTP protocol version.

The version string MUST contain only the HTTP version number (e.g., "1.1", "1.0").

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new protocol version.

Parameters

string $version

HTTP protocol version

Returns

static

getHeaders()

getHeaders() : array

Retrieves all message headers.

The keys represent the header name as it will be sent over the wire, and each value is an array of strings associated with the header.

// Represent the headers as a string
foreach ($message->getHeaders() as $name => $values) {
    echo $name . ": " . implode(", ", $values);
}

// Emit headers iteratively:
foreach ($message->getHeaders() as $name => $values) {
    foreach ($values as $value) {
        header(sprintf('%s: %s', $name, $value), false);
    }
}

Returns

array —

Returns an associative array of the message's headers. Each key MUST be a header name, and each value MUST be an array of strings.

hasHeader()

hasHeader(string  $header) : boolean

Checks if a header exists by the given case-insensitive name.

Parameters

string $header

Case-insensitive header name.

Returns

boolean —

Returns true if any header names match the given header name using a case-insensitive string comparison. Returns false if no matching header name is found in the message.

getHeader()

getHeader(string  $header) : array<mixed,string>

Retrieves a message header value by the given case-insensitive name.

This method returns an array of all the header values of the given case-insensitive header name.

If the header does not appear in the message, this method MUST return an empty array.

Parameters

string $header

Case-insensitive header field name.

Returns

array<mixed,string> —

An array of string values as provided for the given header. If the header does not appear in the message, this method MUST return an empty array.

getHeaderLine()

getHeaderLine(string  $name) : string

Retrieves a comma-separated string of the values for a single header.

This method returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma.

NOTE: Not all header values may be appropriately represented using comma concatenation. For such headers, use getHeader() instead and supply your own delimiter when concatenating.

If the header does not appear in the message, this method MUST return an empty string.

Parameters

string $name

Case-insensitive header field name.

Returns

string —

A string of values as provided for the given header concatenated together using a comma. If the header does not appear in the message, this method MUST return an empty string.

withHeader()

withHeader(string  $header, string|array<mixed,string>  $value) : static

Return an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name.

While header names are case-insensitive, the casing of the header will be preserved by this function, and returned from getHeaders().

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new and/or updated header and value.

Parameters

string $header

Case-insensitive header field name.

string|array<mixed,string> $value

Header value(s).

Throws

\InvalidArgumentException

for invalid header names or values.

Returns

static

withAddedHeader()

withAddedHeader(string  $header, string|array<mixed,string>  $value) : static

Return an instance with the specified header appended with the given value.

Existing values for the specified header will be maintained. The new value(s) will be appended to the existing list. If the header did not exist previously, it will be added.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new header and/or value.

Parameters

string $header

Case-insensitive header field name to add.

string|array<mixed,string> $value

Header value(s).

Throws

\InvalidArgumentException

for invalid header names or values.

Returns

static

withoutHeader()

withoutHeader(string  $header) : static

Return an instance without the specified header.

Header resolution MUST be done without case-sensitivity.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that removes the named header.

Parameters

string $header

Case-insensitive header field name to remove.

Returns

static

withBody()

withBody(\Psr\Http\Message\StreamInterface  $body) : static

Return an instance with the specified message body.

The body MUST be a StreamInterface object.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return a new instance that has the new body stream.

Parameters

\Psr\Http\Message\StreamInterface $body

Body.

Throws

\InvalidArgumentException

When the body is not valid.

Returns

static

headers()

headers() : array

Get all headers

Returns

array

cookies()

cookies() : array

Get all cookies

Returns

array

body()

body(callable|null  $parser = null) : mixed

Get the response body.

By passing in a $parser callable, you can get the decoded response content back.

For example to get the json data as an object:

$body = $response->body('json_decode');

Parameters

callable|null $parser

The callback to use to decode the response body.

Returns

mixed —

The response body.

__construct()

__construct(array  $headers = array(), string  $body = '') 

Constructor

Parameters

array $headers

Unparsed headers.

string $body

The response body.

isOk()

isOk() : boolean

Check if the response was OK

Returns

boolean

isRedirect()

isRedirect() : boolean

Check if the response had a redirect status code.

Returns

boolean

statusCode()

statusCode() : integer

Get the status code from the response

Returns

integer

getStatusCode()

getStatusCode() : integer

Gets the response status code.

The status code is a 3-digit integer result code of the server's attempt to understand and satisfy the request.

Returns

integer —

The status code.

withStatus()

withStatus(integer  $code, string  $reasonPhrase = '') : $this

Return an instance with the specified status code and, optionally, reason phrase.

If no reason phrase is specified, implementations MAY choose to default to the RFC 7231 or IANA recommended reason phrase for the response's status code.

This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the updated status and reason phrase.

Parameters

integer $code

The status code to set.

string $reasonPhrase

The status reason phrase.

Returns

$this —

A copy of the current object with an updated status code.

getReasonPhrase()

getReasonPhrase() : string

Gets the response reason phrase associated with the status code.

Because a reason phrase is not a required element in a response status line, the reason phrase value MAY be null. Implementations MAY choose to return the default RFC 7231 recommended reason phrase (or those listed in the IANA HTTP Status Code Registry) for the response's status code.

Returns

string —

The current reason phrase.

encoding()

encoding() : string|null

Get the encoding if it was set.

Returns

string|null

getEncoding()

getEncoding() : string|null

Get the encoding if it was set.

Returns

string|null

header()

header(string|null  $name = null) : mixed

Read single/multiple header value(s) out.

Parameters

string|null $name

The name of the header you want. Leave null to get all headers.

Returns

mixed —

Null when the header doesn't exist. An array will be returned when getting all headers or when getting a header that had multiple values set. Otherwise a string will be returned.

cookie()

cookie(string|null  $name = null, boolean  $all = false) : mixed

Read single/multiple cookie values out.

Note This method will only provide access to cookies that were added as part of the constructor. If cookies are added post construction they will not be accessible via this method.

Parameters

string|null $name

The name of the cookie you want. Leave null to get all cookies.

boolean $all

Get all parts of the cookie. When false only the value will be returned.

Returns

mixed

getCookies()

getCookies() : array

Get the all cookie data.

Returns

array —

The cookie data

getCookieCollection()

getCookieCollection() : \Cake\Http\Cookie\CookieCollection

Get the cookie collection from this response.

This method exposes the response's CookieCollection instance allowing you to interact with cookie objects directly.

Returns

\Cake\Http\Cookie\CookieCollection

getCookie()

getCookie(string  $name) : string|array|null

Get the value of a single cookie.

Parameters

string $name

The name of the cookie value.

Returns

string|array|null —

Either the cookie's value or null when the cookie is undefined.

getCookieData()

getCookieData(string  $name) : array|null

Get the full data for a single cookie.

Parameters

string $name

The name of the cookie value.

Returns

array|null —

Either the cookie's data or null when the cookie is undefined.

version()

version() : string

Get the HTTP version used.

Returns

string

getStringBody()

getStringBody() : string

Get the response body as string.

Returns

string

getJson()

getJson() : array|null

Get the response body as JSON decoded data.

Returns

array|null

getXml()

getXml() : null|\SimpleXMLElement

Get the response body as XML decoded data.

Returns

null|\SimpleXMLElement

__get()

__get(string  $name) : mixed

Read values as properties.

Parameters

string $name

Property name.

Returns

mixed

__isset()

__isset(string  $name) : boolean

isset/empty test with -> syntax.

Parameters

string $name

Property name.

Returns

boolean

_decodeGzipBody()

_decodeGzipBody(string  $body) : string

Uncompress a gzip response.

Looks for gzip signatures, and if gzinflate() exists, the body will be decompressed.

Parameters

string $body

Gzip encoded body.

Throws

\RuntimeException

When attempting to decode gzip content without gzinflate.

Returns

string

_parseHeaders()

_parseHeaders(array  $headers) : void

Parses headers if necessary.

  • Decodes the status code and reasonphrase.
  • Parses and normalizes header names + values.

Parameters

array $headers

Headers to parse.

convertCookieToArray()

convertCookieToArray(\Cake\Http\Cookie\CookieInterface  $cookie) : array

Convert the cookie into an array of its properties.

This method is compatible with older client code that expects date strings instead of timestamps.

Parameters

\Cake\Http\Cookie\CookieInterface $cookie

Cookie object.

Returns

array

buildCookieCollection()

buildCookieCollection() : void

Lazily build the CookieCollection and cookie objects from the response header

_getCookies()

_getCookies() : array

Property accessor for `$this->cookies`

Returns

array —

Array of Cookie data.

_getJson()

_getJson() : array|null

Get the response body as JSON decoded data.

Returns

array|null

_getXml()

_getXml() : null|\SimpleXMLElement

Get the response body as XML decoded data.

Returns

null|\SimpleXMLElement

_getHeaders()

_getHeaders() : array

Provides magic __get() support.

Returns

array

_getBody()

_getBody() : string

Provides magic __get() support.

Returns

string

getStream()

getStream(  $stream,   $modeIfNotInstance) 

Parameters

$stream
$modeIfNotInstance

setHeaders()

setHeaders(array  $originalHeaders) 

Filter a set of headers to ensure they are in the correct internal format.

Used by message constructors to allow setting all initial headers at once.

Parameters

array $originalHeaders

Headers to filter.

validateProtocolVersion()

validateProtocolVersion(string  $version) 

Validate the HTTP protocol version

Parameters

string $version

Throws

\InvalidArgumentException

on invalid HTTP protocol version

filterHeaderValue()

filterHeaderValue(mixed  $values) : array<mixed,string>

Parameters

mixed $values

Returns

array<mixed,string>

assertHeader()

assertHeader(string  $name) 

Ensure header name and values are valid.

Parameters

string $name

Throws

\InvalidArgumentException