Class Response

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();
Namespace: Cake\Http\Client

Constants summary

  • string
    METHOD_DELETE
    'DELETE'
  • string
    METHOD_GET
    'GET'
  • string
    METHOD_HEAD
    'HEAD'
  • string
    METHOD_OPTIONS
    'OPTIONS'
  • string
    METHOD_PATCH
    'PATCH'
  • string
    METHOD_POST
    'POST'
  • string
    METHOD_PUT
    'PUT'
  • string
    METHOD_TRACE
    'TRACE'
  • int
    STATUS_ACCEPTED
    202
  • int
    STATUS_CREATED
    201
  • int
    STATUS_FOUND
    302
  • int
    STATUS_MOVED_PERMANENTLY
    301
  • int
    STATUS_NON_AUTHORITATIVE_INFORMATION
    203
  • int
    STATUS_NO_CONTENT
    204
  • int
    STATUS_OK
    200
  • int
    STATUS_SEE_OTHER
    303
  • int
    STATUS_TEMPORARY_REDIRECT
    307

Properties summary

  • $_cookies protected
    array

    The array of cookies in the response.

  • $_json protected
    array

    Cached decoded JSON data.

  • $_xml protected
    \SimpleXMLElement

    Cached decoded XML data.

  • $code protected
    int

    The status code of the response.

  • $cookies protected
    \Cake\Http\Cookie\CookieCollection

    Cookie Collection instance

  • $headerNames protected
    array

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

  • $headers protected
    array

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

  • $reasonPhrase protected
    string

    The reason phrase for the status code

Method Summary

  • __construct() public

    Constructor

  • _decodeGzipBody() protected

    Uncompress a gzip response.

  • _getBody() protected

    Provides magic __get() support.

  • _getCookies() protected

    Property accessor for $this->cookies

  • _getHeaders() protected

    Provides magic __get() support.

  • _getJson() protected

    Get the response body as JSON decoded data.

  • _getXml() protected

    Get the response body as XML decoded data.

  • _parseHeaders() protected

    Parses headers if necessary.

  • buildCookieCollection() protected

    Lazily build the CookieCollection and cookie objects from the response header

  • cookies() public

    Get all cookies

  • getBody() public

    Gets the body of the message.

  • getCookie() public

    Get the value of a single cookie.

  • getCookieCollection() public

    Get the cookie collection from this response.

  • getCookieData() public

    Get the full data for a single cookie.

  • getCookies() public

    Get the all cookie data.

  • getEncoding() public

    Get the encoding if it was set.

  • getHeader() public

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

  • getHeaderLine() public

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

  • getHeaders() public

    Retrieves all message headers.

  • getJson() public

    Get the response body as JSON decoded data.

  • getProtocolVersion() public

    Retrieves the HTTP protocol version as a string.

  • getReasonPhrase() public

    Gets the response reason phrase associated with the status code.

  • getStatusCode() public

    Gets the response status code.

  • getStringBody() public

    Get the response body as string.

  • getXml() public

    Get the response body as XML decoded data.

  • hasHeader() public

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

  • isOk() public

    Check if the response status code was in the 2xx/3xx range

  • isRedirect() public

    Check if the response had a redirect status code.

  • isSuccess() public

    Check if the response status code was in the 2xx range

  • withAddedHeader() public

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

  • withBody() public

    Return an instance with the specified message body.

  • withHeader() public

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

  • withProtocolVersion() public

    Return an instance with the specified HTTP protocol version.

  • withStatus() public

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

  • withoutHeader() public

    Return an instance without the specified header.

Method Detail

__construct() public

__construct(array $headers, string $body)

Constructor

Parameters

array $headers optional

Unparsed headers.

string $body optional

The response body.

_decodeGzipBody() protected

_decodeGzipBody(string $body)

Uncompress a gzip response.

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

Parameters

string $body

Gzip encoded body.

Returns

string

Throws

RuntimeException
When attempting to decode gzip content without gzinflate.

_getBody() protected

_getBody()

Provides magic __get() support.

Returns

string

_getCookies() protected

_getCookies()

Property accessor for $this->cookies

Returns

array

Array of Cookie data.

_getHeaders() protected

_getHeaders()

Provides magic __get() support.

Returns

string[]

_getJson() protected

_getJson()

Get the response body as JSON decoded data.

Returns

mixed

_getXml() protected

_getXml()

Get the response body as XML decoded data.

Returns

\SimpleXMLElement|null

_parseHeaders() protected

_parseHeaders(array $headers)

Parses headers if necessary.

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

Parameters

array $headers

Headers to parse.

buildCookieCollection() protected

buildCookieCollection()

Lazily build the CookieCollection and cookie objects from the response header

cookies() public

cookies()

Get all cookies

Returns

array

getBody() public

getBody()

Gets the body of the message.

Returns

\Psr\Http\Message\StreamInterface

Returns the body as a stream.

getCookie() public

getCookie(string $name)

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.

getCookieCollection() public

getCookieCollection()

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

getCookieData() public

getCookieData(string $name)

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.

getCookies() public

getCookies()

Get the all cookie data.

Returns

array

The cookie data

getEncoding() public

getEncoding()

Get the encoding if it was set.

Returns

string|null

getHeader() public

getHeader(mixed $header)

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

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() public

getHeaderLine(mixed $name)

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.

getHeaders() public

getHeaders()

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.

getJson() public

getJson()

Get the response body as JSON decoded data.

Returns

mixed

getProtocolVersion() public

getProtocolVersion()

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.

getReasonPhrase() public

getReasonPhrase()

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.

Links

getStatusCode() public

getStatusCode()

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

int

The status code.

getStringBody() public

getStringBody()

Get the response body as string.

Returns

string

getXml() public

getXml()

Get the response body as XML decoded data.

Returns

\SimpleXMLElement|null

hasHeader() public

hasHeader(mixed $header)

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

Parameters

string $header

Case-insensitive header name.

Returns

bool

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.

isOk() public

isOk()

Check if the response status code was in the 2xx/3xx range

Returns

bool

isRedirect() public

isRedirect()

Check if the response had a redirect status code.

Returns

bool

isSuccess() public

isSuccess()

Check if the response status code was in the 2xx range

Returns

bool

withAddedHeader() public

withAddedHeader(mixed $header, mixed $value)

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|string[] $value

Header value(s).

Returns

static

Throws

Laminas\Diactoros\Exception\InvalidArgumentException
for invalid header names or values.

withBody() public

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

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.

Returns

static

Throws

Laminas\Diactoros\Exception\InvalidArgumentException
When the body is not valid.

withHeader() public

withHeader(mixed $header, mixed $value)

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|string[] $value

Header value(s).

Returns

static

Throws

Laminas\Diactoros\Exception\InvalidArgumentException
for invalid header names or values.

withProtocolVersion() public

withProtocolVersion(mixed $version)

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

withStatus() public

withStatus(mixed $code, mixed $reasonPhrase)

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

int $code

The status code to set.

string $reasonPhrase optional

The status reason phrase.

Returns

static

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

Throws

InvalidArgumentException
For invalid status code arguments.

Links

withoutHeader() public

withoutHeader(mixed $header)

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

Property Detail

$_cookies protected

The array of cookies in the response.

Type

array

$_json protected

Cached decoded JSON data.

Type

array

$_xml protected

Cached decoded XML data.

Type

\SimpleXMLElement

$code protected

The status code of the response.

Type

int

$cookies protected

Cookie Collection instance

Type

\Cake\Http\Cookie\CookieCollection

$headerNames protected

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

Type

array

$headers protected

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

Type

array

$reasonPhrase protected

The reason phrase for the status code

Type

string

© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.1/class-Cake.Http.Client.Response.html