CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.1 Red Velvet API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 3.1
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Auth
    • Cache
    • Collection
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Filesystem
    • Form
    • I18n
    • Log
    • Mailer
    • Network
      • Exception
      • Http
        • Adapter
        • Auth
        • FormData
      • Session
    • ORM
    • Routing
    • Shell
    • TestSuite
    • Utility
    • Validation
    • View

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;
Namespace: Cake\Network\Http

Constants

  • string
    METHOD_DELETE ¶
    'DELETE'

    HTTP DELETE method

  • string
    METHOD_GET ¶
    'GET'

    HTTP GET method

  • string
    METHOD_HEAD ¶
    'HEAD'

    HTTP HEAD method

  • string
    METHOD_OPTIONS ¶
    'OPTIONS'

    HTTP OPTIONS method

  • string
    METHOD_PATCH ¶
    'PATCH'

    HTTP PATCH method

  • string
    METHOD_POST ¶
    'POST'

    HTTP POST method

  • string
    METHOD_PUT ¶
    'PUT'

    HTTP PUT method

  • string
    METHOD_TRACE ¶
    'TRACE'

    HTTP TRACE method

  • int
    STATUS_ACCEPTED ¶
    202

    HTTP 202 code

  • int
    STATUS_CREATED ¶
    201

    HTTP 201 code

  • int
    STATUS_FOUND ¶
    302

    HTTP 302 code

  • int
    STATUS_MOVED_PERMANENTLY ¶
    301

    HTTP 301 code

  • int
    STATUS_OK ¶
    200

    HTTP 200 code

  • int
    STATUS_SEE_OTHER ¶
    303

    HTTP 303 code

  • int
    STATUS_TEMPORARY_REDIRECT ¶
    307

    HTTP 307 code

Property Summary

  • $_body protected
    string

    The response body

  • $_code protected
    int

    The status code of the response.

  • $_cookies protected
    array

    The array of cookies in the response.

  • $_exposedProperties protected
    array

    Map of public => property names for __get()

  • $_headers protected
    array

    The array of headers in the response.

  • $_json protected
    array

    Cached decoded JSON data.

  • $_version protected
    string

    HTTP Version being used.

  • $_xml protected
    SimpleXMLElement

    Cached decoded XML data.

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.

  • _normalizeHeader() protected

    Normalize header names to Camel-Case form.

  • _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.

  • cookies() public

    Get all cookies

  • encoding() public

    Get the encoding if it was set.

  • header() public

    Read single/multiple header value(s) out.

  • headers() public

    Get all headers

  • 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

  • version() public

    Get the HTTP version used.

Method Detail

__construct() ¶ public

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

Constructor

Parameters
array $headers optional

Unparsed headers.

string $body optional

The response body.

__get() ¶ public

__get(string $name): mixed

Read values as properties.

Parameters
string $name

Property name.

Returns
mixed

__isset() ¶ public

__isset(string $name): bool

isset/empty test with -> syntax.

Parameters
string $name

Property name.

Returns
bool

_decodeGzipBody() ¶ protected

_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.

Returns
string
Throws
RuntimeException
When attempting to decode gzip content without gzinflate.

_getJson() ¶ protected

_getJson(): null|array

Get the response body as JSON decoded data.

Returns
null|array

_getXml() ¶ protected

_getXml(): null|SimpleXMLElement

Get the response body as XML decoded data.

Returns
null|SimpleXMLElement

_normalizeHeader() ¶ protected

_normalizeHeader(string $name): string

Normalize header names to Camel-Case form.

Parameters
string $name

The header name to normalize.

Returns
string

_parseCookie() ¶ protected

_parseCookie(string $value): void

Parse a cookie header into data.

Parameters
string $value

The cookie value to parse.

Returns
void

_parseHeaders() ¶ protected

_parseHeaders(array $headers): void

Parses headers if necessary.

  • Decodes the status code.
  • Parses and normalizes header names + values.
Parameters
array $headers

Headers to parse.

Returns
void

body() ¶ public

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 optional

The callback to use to decode the response body.

Returns
mixed

cookie() ¶ public

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

Read single/multiple cookie values out.

Parameters
string|null $name optional

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

bool $all optional

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

Returns
mixed

cookies() ¶ public

cookies(): array

Get all cookies

Returns
array

encoding() ¶ public

encoding(): string|null

Get the encoding if it was set.

Returns
string|null

header() ¶ public

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

Read single/multiple header value(s) out.

Parameters
string|null $name optional

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

Returns
mixed

headers() ¶ public

headers(): array

Get all headers

Returns
array

isOk() ¶ public

isOk(): bool

Check if the response was OK

Returns
bool

isRedirect() ¶ public

isRedirect(): bool

Check if the response had a redirect status code.

Returns
bool

statusCode() ¶ public

statusCode(): int

Get the status code from the response

Returns
int

version() ¶ public

version(): string

Get the HTTP version used.

Returns
string

Property Detail

$_body ¶ protected

The response body

Type
string

$_code ¶ protected

The status code of the response.

Type
int

$_cookies ¶ protected

The array of cookies in the response.

Type
array

$_exposedProperties ¶ protected

Map of public => property names for __get()

Type
array

$_headers ¶ protected

The array of headers in the response.

Type
array

$_json ¶ protected

Cached decoded JSON data.

Type
array

$_version ¶ protected

HTTP Version being used.

Type
string

$_xml ¶ protected

Cached decoded XML data.

Type
SimpleXMLElement
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs