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();Constants
- 
          
          stringMETHOD_DELETE ¶'DELETE'HTTP DELETE method 
- 
          
          stringMETHOD_GET ¶'GET'HTTP GET method 
- 
          
          stringMETHOD_HEAD ¶'HEAD'HTTP HEAD method 
- 
          
          stringMETHOD_OPTIONS ¶'OPTIONS'HTTP OPTIONS method 
- 
          
          stringMETHOD_PATCH ¶'PATCH'HTTP PATCH method 
- 
          
          stringMETHOD_POST ¶'POST'HTTP POST method 
- 
          
          stringMETHOD_PUT ¶'PUT'HTTP PUT method 
- 
          
          stringMETHOD_TRACE ¶'TRACE'HTTP TRACE method 
- 
          
          intSTATUS_ACCEPTED ¶202HTTP 202 code 
- 
          
          intSTATUS_CREATED ¶201HTTP 201 code 
- 
          
          intSTATUS_FOUND ¶302HTTP 302 code 
- 
          
          intSTATUS_MOVED_PERMANENTLY ¶301HTTP 301 code 
- 
          
          intSTATUS_NON_AUTHORITATIVE_INFORMATION ¶203HTTP 203 code 
- 
          
          intSTATUS_NO_CONTENT ¶204HTTP 204 code 
- 
          
          intSTATUS_OK ¶200HTTP 200 code 
- 
          
          intSTATUS_SEE_OTHER ¶303HTTP 303 code 
- 
          
          intSTATUS_TEMPORARY_REDIRECT ¶307HTTP 307 code 
Property Summary
- 
        $_body protectedstring|nullBody for the message. 
- 
        $_cookies protectedarrayThe array of cookies in the response. 
- 
        $_deprecatedMagicProperties protectedarrayMap of deprecated magic properties. 
- 
        $_exposedProperties protectedarrayMap of public => property names for __get() 
- 
        $_json protectedarrayCached decoded JSON data. 
- 
        $_xml protectedSimpleXMLElementCached decoded XML data. 
- 
        $code protectedintThe status code of the response. 
- 
        $cookies protectedCake\Http\Cookie\CookieCollectionCookie Collection instance 
- 
        $headerNames protectedarrayMap of normalized header name to original name used to register header. 
- 
        $headers protectedarrayList of all registered headers, as key => array of values. 
- 
        $reasonPhrase protectedstringThe reason phrase for the status code 
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() public deprecatedGet 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() public deprecatedRead single/multiple cookie values out. 
- 
          cookies() publicGet all cookies 
- 
          encoding() public deprecatedGet the encoding if it was set. 
- 
          getBody() publicGets the body of the message. 
- 
          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. 
- 
          getHeader() publicRetrieves a message header value by the given case-insensitive name. 
- 
          getHeaderLine() publicRetrieves a comma-separated string of the values for a single header. 
- 
          getHeaders() publicRetrieves all message headers. 
- 
          getJson() publicGet the response body as JSON decoded data. 
- 
          getProtocolVersion() publicRetrieves the HTTP protocol version as a string. 
- 
          getReasonPhrase() publicGets the response reason phrase associated with the status code. 
- 
          getStatusCode() publicGets the response status code. 
- 
          getStringBody() publicGet the response body as string. 
- 
          getXml() publicGet the response body as XML decoded data. 
- 
          hasHeader() publicChecks if a header exists by the given case-insensitive name. 
- 
          header() public deprecatedRead single/multiple header value(s) out. 
- 
          headers() public deprecatedGet all headers 
- 
          isOk() publicCheck if the response was OK 
- 
          isRedirect() publicCheck if the response had a redirect status code. 
- 
          isSuccess() publicCheck if the response status code was in the 2xx range 
- 
          statusCode() public deprecatedGet the status code from the response 
- 
          version() public deprecatedGet the HTTP version used. 
- 
          withAddedHeader() publicReturn an instance with the specified header appended with the given value. 
- 
          withBody() publicReturn an instance with the specified message body. 
- 
          withHeader() publicReturn an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name. 
- 
          withProtocolVersion() publicReturn an instance with the specified HTTP protocol version. 
- 
          withStatus() publicReturn an instance with the specified status code and, optionally, reason phrase. 
- 
          withoutHeader() publicReturn an instance without the specified header. 
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): mixedRead values as properties.
Parameters
- 
                string$name
- Property name. 
Returns
mixed__isset() ¶ public
__isset(string $name): boolisset/empty test with -> syntax.
Parameters
- 
                string$name
- Property name. 
Returns
bool_decodeGzipBody() ¶ protected
_decodeGzipBody(string $body): stringUncompress 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() ¶ protected
_getCookies(): arrayProperty accessor for $this->cookies
Returns
arrayArray of Cookie data.
_getJson() ¶ protected
_getJson(): array|nullGet the response body as JSON decoded data.
Returns
array|null_getXml() ¶ protected
_getXml(): SimpleXMLElement|nullGet the response body as XML decoded data.
Returns
SimpleXMLElement|null_parseHeaders() ¶ protected
_parseHeaders(array $headers): voidParses headers if necessary.
- Decodes the status code and reasonphrase.
- Parses and normalizes header names + values.
Parameters
- 
                array$headers
- Headers to parse. 
Returns
voidbody() ¶ public
body(callable|null $parser = null): mixedGet 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->getJson();Parameters
- 
                callable|null$parser optional
- The callback to use to decode the response body. 
Returns
mixedThe response body.
buildCookieCollection() ¶ protected
buildCookieCollection(): voidLazily build the CookieCollection and cookie objects from the response header
Returns
voidconvertCookieToArray() ¶ protected
convertCookieToArray(Cake\Http\Cookie\CookieInterface $cookie): arrayConvert 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() ¶ public
cookie(string|null $name = null, bool $all = false): mixedRead 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.
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
mixedgetBody() ¶ public
getBody(): StreamInterfaceGets the body of the message.
Returns
StreamInterfaceReturns the body as a stream.
getCookie() ¶ public
getCookie(string $name): string|array|nullGet the value of a single cookie.
Parameters
- 
                string$name
- The name of the cookie value. 
Returns
string|array|nullEither the cookie's value or null when the cookie is undefined.
getCookieCollection() ¶ public
getCookieCollection(): Cake\Http\Cookie\CookieCollectionGet 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\CookieCollectiongetCookieData() ¶ public
getCookieData(string $name): array|nullGet 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.
getEncoding() ¶ public
getEncoding(): string|nullGet the encoding if it was set.
Returns
string|nullgetHeader() ¶ public
getHeader(string $header): string[]Retrieves a message header value by the given case-insensitive name.
This method returns an array of all the header values of the given case-insensitive header name.
If the header does not appear in the message, this method MUST return an empty array.
Parameters
- 
                string$header
- Case-insensitive header field name. 
Returns
string[]An array of string values as provided for the given header. If the header does not appear in the message, this method MUST return an empty array.
getHeaderLine() ¶ public
getHeaderLine(string $name): stringRetrieves a comma-separated string of the values for a single header.
This method returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma.
NOTE: Not all header values may be appropriately represented using comma concatenation. For such headers, use getHeader() instead and supply your own delimiter when concatenating.
If the header does not appear in the message, this method MUST return an empty string.
Parameters
- 
                string$name
- Case-insensitive header field name. 
Returns
stringA string of values as provided for the given header concatenated together using a comma. If the header does not appear in the message, this method MUST return an empty string.
getHeaders() ¶ public
getHeaders(): arrayRetrieves all message headers.
The keys represent the header name as it will be sent over the wire, and each value is an array of strings associated with the header.
// Represent the headers as a string foreach ($message->getHeaders() as $name => $values) { echo $name . ": " . implode(", ", $values); }
// Emit headers iteratively: foreach ($message->getHeaders() as $name => $values) { foreach ($values as $value) { header(sprintf('%s: %s', $name, $value), false); } }
Returns
arrayReturns an associative array of the message's headers. Each key MUST be a header name, and each value MUST be an array of strings.
getJson() ¶ public
getJson(): array|nullGet the response body as JSON decoded data.
Returns
array|nullgetProtocolVersion() ¶ public
getProtocolVersion(): stringRetrieves the HTTP protocol version as a string.
The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
Returns
stringHTTP protocol version.
getReasonPhrase() ¶ public
getReasonPhrase(): stringGets the response reason phrase associated with the status code.
Because a reason phrase is not a required element in a response status line, the reason phrase value MAY be null. Implementations MAY choose to return the default RFC 7231 recommended reason phrase (or those listed in the IANA HTTP Status Code Registry) for the response's status code.
Returns
stringThe current reason phrase.
getStatusCode() ¶ public
getStatusCode(): intGets the response status code.
The status code is a 3-digit integer result code of the server's attempt to understand and satisfy the request.
Returns
intThe status code.
getXml() ¶ public
getXml(): SimpleXMLElement|nullGet the response body as XML decoded data.
Returns
SimpleXMLElement|nullhasHeader() ¶ public
hasHeader(string $header): boolChecks if a header exists by the given case-insensitive name.
Parameters
- 
                string$header
- Case-insensitive header name. 
Returns
boolReturns true if any header names match the given header name using a case-insensitive string comparison. Returns false if no matching header name is found in the message.
header() ¶ public
header(string|null $name = null): mixedRead 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
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() ¶ public
isRedirect(): boolCheck if the response had a redirect status code.
Returns
boolisSuccess() ¶ public
isSuccess(): boolCheck if the response status code was in the 2xx range
Returns
boolwithAddedHeader() ¶ public
withAddedHeader(string $header, string|string[] $value): staticReturn an instance with the specified header appended with the given value.
Existing values for the specified header will be maintained. The new value(s) will be appended to the existing list. If the header did not exist previously, it will be added.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new header and/or value.
Parameters
- 
                string$header
- Case-insensitive header field name to add. 
- 
                string|string[]$value
- Header value(s). 
Returns
staticThrows
InvalidArgumentExceptionfor invalid header names or values.
withBody() ¶ public
withBody(StreamInterface $body): staticReturn an instance with the specified message body.
The body MUST be a StreamInterface object.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return a new instance that has the new body stream.
Parameters
- 
                StreamInterface$body
- Body. 
Returns
staticThrows
InvalidArgumentExceptionWhen the body is not valid.
withHeader() ¶ public
withHeader(string $header, string|string[] $value): staticReturn an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name.
While header names are case-insensitive, the casing of the header will be preserved by this function, and returned from getHeaders().
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new and/or updated header and value.
Parameters
- 
                string$header
- Case-insensitive header field name. 
- 
                string|string[]$value
- Header value(s). 
Returns
staticThrows
InvalidArgumentExceptionfor invalid header names or values.
withProtocolVersion() ¶ public
withProtocolVersion(string $version): staticReturn an instance with the specified HTTP protocol version.
The version string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new protocol version.
Parameters
- 
                string$version
- HTTP protocol version 
Returns
staticwithStatus() ¶ public
withStatus(int $code, string $reasonPhrase = ''): $thisReturn an instance with the specified status code and, optionally, reason phrase.
If no reason phrase is specified, implementations MAY choose to default to the RFC 7231 or IANA recommended reason phrase for the response's status code.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the updated status and reason phrase.
Parameters
- 
                int$code
- The status code to set. 
- 
                string$reasonPhrase optional
- The status reason phrase. 
Returns
$thisA copy of the current object with an updated status code.
withoutHeader() ¶ public
withoutHeader(string $header): staticReturn an instance without the specified header.
Header resolution MUST be done without case-sensitivity.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that removes the named header.
Parameters
- 
                string$header
- Case-insensitive header field name to remove. 
Returns
staticProperty Detail
$headerNames ¶ protected
Map of normalized header name to original name used to register header.
Type
array