Implements methods for HTTP responses.
All 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();
string 'DELETE'
HTTP DELETE method
string 'GET'
HTTP GET method
string 'HEAD'
HTTP HEAD method
string 'OPTIONS'
HTTP OPTIONS method
string 'PATCH'
HTTP PATCH method
string 'POST'
HTTP POST method
string 'PUT'
HTTP PUT method
string 'TRACE'
HTTP TRACE method
int 202
HTTP 202 code
int 201
HTTP 201 code
int 302
HTTP 302 code
int 301
HTTP 301 code
int 203
HTTP 203 code
int 204
HTTP 204 code
int 200
HTTP 200 code
int 308
HTTP 308 code
int 303
HTTP 303 code
int 307
HTTP 307 code
arrayThe array of cookies in the response.
mixedCached decoded JSON data.
SimpleXMLElementCached decoded XML data.
intThe status code of the response.
Cake\Http\Cookie\CookieCollectionCookie Collection instance
arrayMap of normalized header name to original name used to register header.
arrayList of all registered headers, as key => array of values.
stringThe reason phrase for the status code
Constructor
Uncompress a gzip response.
Provides magic __get() support.
Property accessor for $this->cookies
Provides magic __get() support.
Get the response body as JSON decoded data.
Get the response body as XML decoded data.
Parses headers if necessary.
Lazily build the CookieCollection and cookie objects from the response header
Get all cookies
Gets the body of the message.
Get the value of a single cookie.
Get the cookie collection from this response.
Get the full data for a single cookie.
Get the all cookie data.
Get the encoding if it was set.
Retrieves a message header value by the given case-insensitive name.
Retrieves a comma-separated string of the values for a single header.
Retrieves all message headers.
Get the response body as JSON decoded data.
Retrieves the HTTP protocol version as a string.
Gets the response reason phrase associated with the status code.
Gets the response status code.
Get the response body as string.
Get the response body as XML decoded data.
Checks if a header exists by the given case-insensitive name.
Check if the response status code was in the 2xx/3xx range
Check if the response had a redirect status code.
Check if the response status code was in the 2xx range
Return an instance with the specified header appended with the given value.
Return an instance with the specified message body.
Return an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name.
Return an instance with the specified HTTP protocol version.
Return an instance with the specified status code and, optionally, reason phrase.
Return an instance without the specified header.
__construct(array $headers = [], string $body = '')
Constructor
array $headers optional Unparsed headers.
string $body optional The response body.
_decodeGzipBody(string $body): string
Uncompress a gzip response.
Looks for gzip signatures, and if gzinflate() exists, the body will be decompressed.
string $body Gzip encoded body.
stringRuntimeException_getBody(): string
Provides magic __get() support.
string_getCookies(): array
Property accessor for $this->cookies
array_getHeaders(): array<string>
Provides magic __get() support.
array<string>_getJson(): mixed
Get the response body as JSON decoded data.
mixed_getXml(): SimpleXMLElement|null
Get the response body as XML decoded data.
SimpleXMLElement|null_parseHeaders(array $headers): void
Parses headers if necessary.
array $headers Headers to parse.
voidbuildCookieCollection(): void
Lazily build the CookieCollection and cookie objects from the response header
voidcookies(): array
Get all cookies
arraygetBody(): StreamInterface
Gets the body of the message.
StreamInterfacegetCookie(string $name): array|string|null
Get the value of a single cookie.
string $name The name of the cookie value.
array|string|nullgetCookieCollection(): Cake\Http\Cookie\CookieCollection
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): array|null
Get the full data for a single cookie.
string $name The name of the cookie value.
array|nullgetCookies(): array
Get the all cookie data.
arraygetEncoding(): string|null
Get the encoding if it was set.
string|nullgetHeader(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.
string $header Case-insensitive header field name.
string[]getHeaderLine(string $name): string
Retrieves 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.
string $name Case-insensitive header field name.
stringgetHeaders(): array
Retrieves 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); } }
arraygetJson(): mixed
Get the response body as JSON decoded data.
mixedgetProtocolVersion(): string
Retrieves the HTTP protocol version as a string.
The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
stringgetReasonPhrase(): string
Gets 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.
stringgetStatusCode(): int
Gets 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.
intgetStringBody(): string
Get the response body as string.
stringgetXml(): SimpleXMLElement|null
Get the response body as XML decoded data.
SimpleXMLElement|nullhasHeader(string $header): bool
Checks if a header exists by the given case-insensitive name.
string $header Case-insensitive header name.
boolisOk(): bool
Check if the response status code was in the 2xx/3xx range
boolisRedirect(): bool
Check if the response had a redirect status code.
boolisSuccess(): bool
Check if the response status code was in the 2xx range
boolwithAddedHeader(string $name, string|string[] $value): static
Return 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.
string $name Case-insensitive header field name to add.
string|string[] $value Header value(s).
staticException\InvalidArgumentExceptionwithBody(StreamInterface $body): static
Return 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.
StreamInterface $body Body.
staticException\InvalidArgumentExceptionwithHeader(string $name, string|string[] $value): static
Return 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.
string $name Case-insensitive header field name.
string|string[] $value Header value(s).
staticException\InvalidArgumentExceptionwithProtocolVersion(string $version): static
Return 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.
string $version HTTP protocol version
staticwithStatus(int $code, string $reasonPhrase = ''): static
Return 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.
int $code The status code to set.
string $reasonPhrase optional The status reason phrase.
staticwithoutHeader(string $name): static
Return 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.
string $name Case-insensitive header field name to remove.
staticThe array of cookies in the response.
arrayCached decoded JSON data.
mixedCached decoded XML data.
SimpleXMLElementThe status code of the response.
intCookie Collection instance
Cake\Http\Cookie\CookieCollectionMap of normalized header name to original name used to register header.
arrayList of all registered headers, as key => array of values.
arrayThe reason phrase for the status code
string
© 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/4.4/class-Cake.Http.Client.Response.html