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->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'];
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.
You can access the response status code using:
$content = $response->statusCode();
You can also use object access:
$content = $response->code;
__construct( array $headers [] , string $body '' )
Constructor
$headers
optional [] $body
optional '' __get( string $name )
Read values as properties.
$name
mixed
__isset( string $name )
isset/empty test with -> syntax.
$name
boolean
_decodeGzipBody( string $body )
Uncompress a gzip response.
Looks for gzip signatures, and if gzinflate() exists, the body will be decompressed.
$body
string
RuntimeException
_getJson( )
Get the response body as JSON decoded data.
null|array
_getXml( )
Get the response body as XML decoded data.
null|SimpleXMLElement
_parseCookie( string $value )
Parse a cookie header into data.
$value
_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 mixed
Cake\Network\Http\Message::body()
cookie( string|null $name null , boolean $all false )
Read single/multiple cookie values out.
$name
optional null $all
optional false mixed
header( string|null $name null )
Read single/multiple header value(s) out.
$name
optional null mixed
isRedirect( )
Check if the response had a redirect status code.
boolean
_normalizeHeader( string $name )
Normalize header names to Camel-Case form.
$name
string
© 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.1/class-Cake.Network.Http.Response.html