CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 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

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • CookieCollection
  • FormData
  • Message
  • Request
  • Response

Interfaces

  • AdapterInterface

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();
Cake\Http\Client\Message
Extended by Cake\Http\Client\Response implements Psr\Http\Message\ResponseInterface uses Zend\Diactoros\MessageTrait
Namespace: Cake\Http\Client
Location: Http/Client/Response.php

Inherited Constants

  • METHOD_DELETE, METHOD_GET, METHOD_HEAD, METHOD_OPTIONS, METHOD_PATCH, METHOD_POST, METHOD_PUT, METHOD_TRACE, STATUS_ACCEPTED, STATUS_CREATED, STATUS_FOUND, STATUS_MOVED_PERMANENTLY, STATUS_NON_AUTHORITATIVE_INFORMATION, STATUS_NO_CONTENT, STATUS_OK, STATUS_SEE_OTHER, STATUS_TEMPORARY_REDIRECT

Properties summary

  • $_exposedProperties protected
    array
    Map of public => property names for __get()
  • $_json protected
    array
    Cached decoded JSON data.
  • $_xml protected
    SimpleXMLElement
    Cached decoded XML data.
  • $code protected
    integer
    The status code of the response.
  • $cookies protected
    Cake\Http\Cookie\CookieCollection
    Cookie Collection instance
  • $reasonPhrase protected
    string
    The reason phrase for the status code

Inherited Magic Properties

  • headers

Inherited Properties

  • _body, _cookies

Method Summary

  • __construct() public
    Constructor
  • __get() public
    Read values as properties.
  • __isset() public
    isset/empty test with -> syntax.
  • _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.
  • body() public deprecated
    Get the response body.
  • buildCookieCollection() protected
    Lazily build the CookieCollection and cookie objects from the response header
  • convertCookieToArray() protected
    Convert the cookie into an array of its properties.
  • cookie() public deprecated
    Read single/multiple cookie values out.
  • encoding() public deprecated
    Get the encoding if it was set.
  • 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.
  • getJson() public
    Get the response body as JSON decoded data.
  • getReasonPhrase() public
  • getStatusCode() public
  • getStringBody() public
    Get the response body as string.
  • getXml() public
    Get the response body as XML decoded data.
  • header() public deprecated
    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 deprecated
    Get the status code from the response
  • version() public deprecated
    Get the HTTP version used.
  • withStatus() public

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 )

Read values as properties.

Parameters
string $name
Property name.
Returns
mixed

__isset() public ¶

__isset( string $name )

isset/empty test with -> syntax.

Parameters
string $name
Property name.
Returns
boolean

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

_getJson() protected ¶

_getJson( )

Get the response body as JSON decoded data.

Returns
array|null

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

body() public deprecated ¶

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');
Deprecated
3.7.0 Use getStringBody()/getJson()/getXml() instead.
Parameters
callable|null $parser optional null

The callback to use to decode the response body.

Returns
mixed
The response body.
Overrides
Cake\Http\Client\Message::body()

buildCookieCollection() protected ¶

buildCookieCollection( )

Lazily build the CookieCollection and cookie objects from the response header

convertCookieToArray() protected ¶

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

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

cookie() public deprecated ¶

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

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.

Deprecated
3.3.0 Use getCookie(), getCookieData() or getCookies() instead.
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() public deprecated ¶

encoding( )

Get the encoding if it was set.

Deprecated
3.3.0 Use getEncoding() instead.
Returns
string|null

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

getJson() public ¶

getJson( )

Get the response body as JSON decoded data.

Returns
array|null

getReasonPhrase() public ¶

getReasonPhrase( )

Returns
string
The current reason phrase.

getStatusCode() public ¶

getStatusCode( )

Returns
integer
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

header() public deprecated ¶

header( string|null $name = null )

Read single/multiple header value(s) out.

Deprecated
3.3.0 Use getHeader() and getHeaderLine() instead.
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() public ¶

isOk( )

Check if the response was OK

Returns
boolean

isRedirect() public ¶

isRedirect( )

Check if the response had a redirect status code.

Returns
boolean

statusCode() public deprecated ¶

statusCode( )

Get the status code from the response

Deprecated
3.3.0 Use getStatusCode() instead.
Returns
integer

version() public deprecated ¶

version( )

Get the HTTP version used.

Deprecated
3.3.0 Use getProtocolVersion()
Returns
string

withStatus() public ¶

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

Parameters
integer $code
The status code to set.
string $reasonPhrase optional ''
The status reason phrase.
Returns

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

Methods inherited from Cake\Http\Client\Message

cookies() public ¶

cookies( )

Get all cookies

Returns
array

headers() public deprecated ¶

headers( )

Get all headers

Deprecated
3.3.0 Use getHeaders() instead.
Returns
array

Properties detail

$_exposedProperties ¶

protected array

Map of public => property names for __get()

[
    'cookies' => '_getCookies',
    'body' => '_getBody',
    'code' => 'code',
    'json' => '_getJson',
    'xml' => '_getXml',
    'headers' => '_getHeaders',
]

$_json ¶

protected array

Cached decoded JSON data.

$_xml ¶

protected SimpleXMLElement

Cached decoded XML data.

$code ¶

protected integer

The status code of the response.

$cookies ¶

protected Cake\Http\Cookie\CookieCollection

Cookie Collection instance

$reasonPhrase ¶

protected string

The reason phrase for the status code

Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs