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();
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 stream using:
$content = $response->getBody();
You can also use object access to get the string version of the response body:
$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->getStatusCode();
You can also use object access:
$content = $response->code;
- Cake\Http\Client\Message
- Cake\Http\Client\Response implements Psr\Http\Message\ResponseInterface uses Zend\Diactoros\MessageTrait
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_OK
,STATUS_SEE_OTHER
,STATUS_TEMPORARY_REDIRECT
Properties summary
-
$_exposedProperties
protectedMap of public => property names for __get()array
-
$_json
protectedCached decoded JSON data.array
-
$_xml
protectedCached decoded XML data.SimpleXMLElement
-
$code
protectedThe status code of the response.integer
-
$cookies
protected -
$reasonPhrase
protectedThe reason phrase for the status codestring
Inherited Magic Properties
Inherited Properties
Method Summary
- __construct() publicConstructor
- __get() publicRead values as properties.
- __isset() publicisset/empty test with -> syntax.
- _decodeGzipBody() protectedUncompress a gzip response.
- _getBody() protectedProvides magic __get() support.
- _getCookies() protectedProperty accessor for
$this->cookies
- _getHeaders() protectedProvides magic __get() support.
- _getJson() protectedGet the response body as JSON decoded data.
- _getXml() protectedGet the response body as XML decoded data.
- _parseHeaders() protectedParses headers if necessary.
- body() publicGet the response body.
- buildCookieCollection() protectedLazily build the CookieCollection and cookie objects from the response header
- convertCookieToArray() protectedConvert the cookie into an array of its properties.
- cookie() publicRead single/multiple cookie values out.
- encoding() publicGet the encoding if it was set.
- getCookie() publicGet the value of a single cookie.
- getCookieCollection() publicGet the cookie collection from this response.
- getCookieData() publicGet the full data for a single cookie.
- getCookies() publicGet the all cookie data.
- getEncoding() publicGet the encoding if it was set.
- getReasonPhrase() public
- getStatusCode() public
- header() publicRead single/multiple header value(s) out.
- isOk() publicCheck if the response was OK
- isRedirect() publicCheck if the response had a redirect status code.
- statusCode() publicGet the status code from the response
- version() publicGet the HTTP version used.
- withStatus() public
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
stringThrows
RuntimeExceptionWhen attempting to decode gzip content without gzinflate.
_getCookies()source protected
_getCookies( )
Property accessor for $this->cookies
Returns
arrayArray of Cookie data.
_getJson()source protected
_getJson( )
Get the response body as JSON decoded data.
Returns
array|null_getXml()source protected
_getXml( )
Get the response body as XML decoded data.
Returns
null|SimpleXMLElement_parseHeaders()source 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()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
mixedThe response body.
Overrides
Cake\Http\Client\Message::body()
buildCookieCollection()source protected
buildCookieCollection( )
Lazily build the CookieCollection and cookie objects from the response header
convertCookieToArray()source 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
arraycookie()source public
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
mixedencoding()source public
encoding( )
Get the encoding if it was set.
Deprecated
3.3.0 Use getEncoding() instead.Returns
string|nullgetCookie()source public
getCookie( string $name )
Get the value of a single cookie.
Parameters
- string
$name
- The name of the cookie value.
Returns
string|nullEither the cookie's value or null when the cookie is undefined.
getCookieCollection()source 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()source public
getCookieData( string $name )
Get the full data for a single cookie.
Parameters
- string
$name
- The name of the cookie value.
Returns
array|nullEither the cookie's data or null when the cookie is undefined.
header()source public
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
mixedNull 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()source public
isRedirect( )
Check if the response had a redirect status code.
Returns
booleanstatusCode()source public
statusCode( )
Get the status code from the response
Deprecated
3.3.0 Use getStatusCode() instead.Returns
integerversion()source public
version( )
Get the HTTP version used.
Deprecated
3.3.0 Use getProtocolVersion()Returns
stringwithStatus()source 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
headers()source public
headers( )
Get all headers
Deprecated
3.3.0 Use getHeaders() instead.Returns
arrayProperties detail
$_exposedPropertiessource
protected array
Map of public => property names for __get()
[ 'cookies' => '_getCookies', 'body' => '_getBody', 'code' => 'code', 'json' => '_getJson', 'xml' => '_getXml', 'headers' => '_getHeaders', ]
© 2005–2017 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.4/class-Cake.Http.Client.Response.html