Implements methods for HTTP responses.
All of the following examples assume that $response is an instance of this class.
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();
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.
You can access the response status code using:
$content = $response->getStatusCode();
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 $_exposedProperties protected array$_json protected array$_xml protected SimpleXMLElement$code protected integer$cookies protected $reasonPhrase protected string$this->cookies __construct( array $headers = [] , string $body = '' )
Constructor
$headers optional [] $body optional '' __get( string $name )
Read values as properties.
$name __isset( string $name )
isset/empty test with -> syntax.
$name _decodeGzipBody( string $body )
Uncompress a gzip response.
Looks for gzip signatures, and if gzinflate() exists, the body will be decompressed.
$body _getCookies( )
Property accessor for $this->cookies
_getJson( )
Get the response body as JSON decoded data.
_getXml( )
Get the response body as XML decoded data.
_parseHeaders( array $headers )
Parses headers if necessary.
$headers 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'); $parser optional null The callback to use to decode the response body.
Cake\Http\Client\Message::body() buildCookieCollection( )
Lazily build the CookieCollection and cookie objects from the response header
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.
Cake\Http\Cookie\CookieInterface $cookie 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.
$name optional null The name of the cookie you want. Leave null to get all cookies.
$all optional false Get all parts of the cookie. When false only the value will be returned.
encoding( )
Get the encoding if it was set.
getCookie( string $name )
Get the value of a single cookie.
$name getCookieCollection( )
Get the cookie collection from this response.
This method exposes the response's CookieCollection instance allowing you to interact with cookie objects directly.
Cake\Http\Cookie\CookieCollectiongetCookieData( string $name )
Get the full data for a single cookie.
$name getXml( )
Get the response body as XML decoded data.
header( string|null $name = null )
Read single/multiple header value(s) out.
$name optional null The name of the header you want. Leave null to get all headers.
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.
isRedirect( )
Check if the response had a redirect status code.
statusCode( )
Get the status code from the response
version( )
Get the HTTP version used.
withStatus( integer $code , string $reasonPhrase = '' )
$code $reasonPhrase optional '' headers( )
Get all headers
protected array
Map of public => property names for __get()
[
'cookies' => '_getCookies',
'body' => '_getBody',
'code' => 'code',
'json' => '_getJson',
'xml' => '_getXml',
'headers' => '_getHeaders',
]
© 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/3.8/class-Cake.Http.Client.Response.html