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->header('content-type');

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

$response->header();

You can also get at the headers using object access. When getting headers with object access, you have to use case-sensitive header names:

$val = $response->headers['Content-Type'];

Get the response body

You can access the response body using:

$content = $response->body();

You can also use object access:

$content = $response->body;

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->xml
// Get as json
$content = $response->json

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

Check the status code

You can access the response status code using:

$content = $response->statusCode();

You can also use object access:

$content = $response->code;
Cake\Network\Http\Message
Extended by Cake\Network\Http\Response

Inherited Constants

Properties summary

  • $_body protected
    string
    The response body
  • $_code protected
    integer
    The status code of the response.
  • array
    Map of public => property names for __get()
  • $_json protected
    array
    Cached decoded JSON data.
  • $_xml protected
    SimpleXMLElement
    Cached decoded XML data.

Inherited Properties

Method Summary

  • __construct() public
    Constructor
  • __get() public
    Read values as properties.
  • __isset() public
    isset/empty test with -> syntax.
  • _decodeGzipBody() protected
    Uncompress a gzip response.
  • _getJson() protected
    Get the response body as JSON decoded data.
  • _getXml() protected
    Get the response body as XML decoded data.
  • _parseCookie() protected
    Parse a cookie header into data.
  • _parseHeaders() protected
    Parses headers if necessary.
  • body() public
    Get the response body.
  • cookie() public
    Read single/multiple cookie values out.
  • encoding() public
    Get the encoding if it was set.
  • header() public
    Read single/multiple header value(s) out.
  • isOk() public
    Check if the response was OK
  • isRedirect() public
    Check if the response had a redirect status code.
  • statusCode() public
    Get the status code from the response

Method Detail

__construct()source public

__construct( array $headers [] , string $body '' )

Constructor

Parameters

array $headers optional []
Unparsed headers.
string $body optional ''
The response body.

__get()source public

__get( string $name )

Read values as properties.

Parameters

string $name
Property name.

Returns

mixed

__isset()source public

__isset( string $name )

isset/empty test with -> syntax.

Parameters

string $name
Property name.

Returns

boolean

_decodeGzipBody()source 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.

_getJson()source protected

_getJson( )

Get the response body as JSON decoded data.

Returns

mixed

_getXml()source protected

_getXml( )

Get the response body as XML decoded data.

Returns

null|SimpleXMLElement

_parseCookie()source protected

_parseCookie( string $value )

Parse a cookie header into data.

Parameters

string $value
The cookie value to parse.

_parseHeaders()source protected

_parseHeaders( array $headers )

Parses headers if necessary.

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

Parameters

array $headers
Headers to parse.

body()source public

body( callable|null $parser null )

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 optional null

The callback to use to decode the response body.

Returns

mixed
The response body.

Overrides

Cake\Network\Http\Message::body()
cookie( string|null $name null , boolean $all false )

Read single/multiple cookie values out.

Parameters

string|null $name optional null

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

boolean $all optional false

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

Returns

mixed

encoding()source public

encoding( )

Get the encoding if it was set.

Returns

string|null

header()source public

header( string|null $name null )

Read single/multiple header value(s) out.

Parameters

string|null $name optional null

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.


isOk()source public

isOk( )

Check if the response was OK

Returns

boolean

isRedirect()source public

isRedirect( )

Check if the response had a redirect status code.

Returns

boolean

statusCode()source public

statusCode( )

Get the status code from the response

Returns

integer

Methods inherited from Cake\Network\Http\Message

_normalizeHeader()source protected

_normalizeHeader( string $name )

Normalize header names to Camel-Case form.

Parameters

string $name
The header name to normalize.

Returns

string
Normalized header name.

cookies()source public

cookies( )

Get all cookies

Returns

array

headers()source public

headers( )

Get all headers

Returns

array

version()source public

version( )

Get the HTTP version used.

Returns

string

Properties detail

$_bodysource

protected string

The response body

$_codesource

protected integer

The status code of the response.

$_exposedPropertiessource

protected array

Map of public => property names for __get()

[
    'cookies' => '_cookies',
    'headers' => '_headers',
    'body' => '_body',
    'code' => '_code',
    'json' => '_getJson',
    'xml' => '_getXml'
]

$_jsonsource

protected array

Cached decoded JSON data.

$_xmlsource

protected SimpleXMLElement

Cached decoded XML data.

© 2005–2016 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.
http://api.cakephp.org/3.2/class-Cake.Network.Http.Response.html