Fr3nch13/CakePHP Utilities

Response extends Message
in package
implements ResponseInterface uses MessageTrait

Implements methods for HTTP responses.

All 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();

Table of Contents

Interfaces

ResponseInterface
Representation of an outgoing, server-side response.

Constants

METHOD_DELETE  = 'DELETE'
HTTP DELETE method
METHOD_GET  = 'GET'
HTTP GET method
METHOD_HEAD  = 'HEAD'
HTTP HEAD method
METHOD_OPTIONS  = 'OPTIONS'
HTTP OPTIONS method
METHOD_PATCH  = 'PATCH'
HTTP PATCH method
METHOD_POST  = 'POST'
HTTP POST method
METHOD_PUT  = 'PUT'
HTTP PUT method
METHOD_TRACE  = 'TRACE'
HTTP TRACE method
STATUS_ACCEPTED  = 202
HTTP 202 code
STATUS_CREATED  = 201
HTTP 201 code
STATUS_FOUND  = 302
HTTP 302 code
STATUS_MOVED_PERMANENTLY  = 301
HTTP 301 code
STATUS_NO_CONTENT  = 204
HTTP 204 code
STATUS_NON_AUTHORITATIVE_INFORMATION  = 203
HTTP 203 code
STATUS_OK  = 200
HTTP 200 code
STATUS_PERMANENT_REDIRECT  = 308
HTTP 308 code
STATUS_SEE_OTHER  = 303
HTTP 303 code
STATUS_TEMPORARY_REDIRECT  = 307
HTTP 307 code

Properties

$_cookies  : array<string|int, mixed>
The array of cookies in the response.
$_json  : mixed
Cached decoded JSON data.
$_xml  : SimpleXMLElement
Cached decoded XML data.
$code  : int
The status code of the response.
$cookies  : CookieCollection
Cookie Collection instance
$headerNames  : array<string|int, mixed>
Map of normalized header name to original name used to register header.
$headers  : array<string|int, mixed>
List of all registered headers, as key => array of values.
$reasonPhrase  : string
The reason phrase for the status code
$protocol  : string
$stream  : StreamInterface

Methods

__construct()  : mixed
Constructor
cookies()  : array<string|int, mixed>
Get all cookies
getBody()  : StreamInterface
Gets the body of the message.
getCookie()  : array<string|int, mixed>|string|null
Get the value of a single cookie.
getCookieCollection()  : CookieCollection
Get the cookie collection from this response.
getCookieData()  : array<string|int, mixed>|null
Get the full data for a single cookie.
getCookies()  : array<string|int, mixed>
Get the all cookie data.
getEncoding()  : string|null
Get the encoding if it was set.
getHeader()  : array<string|int, string>
Retrieves a message header value by the given case-insensitive name.
getHeaderLine()  : string
Retrieves a comma-separated string of the values for a single header.
getHeaders()  : array<string|int, mixed>
Retrieves all message headers.
getJson()  : mixed
Get the response body as JSON decoded data.
getProtocolVersion()  : string
Retrieves the HTTP protocol version as a string.
getReasonPhrase()  : string
Gets the response reason phrase associated with the status code.
getStatusCode()  : int
Gets the response status code.
getStringBody()  : string
Get the response body as string.
getXml()  : SimpleXMLElement|null
Get the response body as XML decoded data.
hasHeader()  : bool
Checks if a header exists by the given case-insensitive name.
isOk()  : bool
Check if the response status code was in the 2xx/3xx range
isRedirect()  : bool
Check if the response had a redirect status code.
isSuccess()  : bool
Check if the response status code was in the 2xx range
withAddedHeader()  : static
Return an instance with the specified header appended with the given value.
withBody()  : static
Return an instance with the specified message body.
withHeader()  : static
Return an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name.
withoutHeader()  : static
Return an instance without the specified header.
withProtocolVersion()  : static
Return an instance with the specified HTTP protocol version.
withStatus()  : static
Return an instance with the specified status code and, optionally, reason phrase.
_decodeGzipBody()  : string
Uncompress a gzip response.
_getBody()  : string
Provides magic __get() support.
_getCookies()  : array<string|int, mixed>
Property accessor for `$this->cookies`
_getHeaders()  : array<string|int, string>
Provides magic __get() support.
_getJson()  : mixed
Get the response body as JSON decoded data.
_getXml()  : SimpleXMLElement|null
Get the response body as XML decoded data.
_parseHeaders()  : void
Parses headers if necessary.
buildCookieCollection()  : void
Lazily build the CookieCollection and cookie objects from the response header
assertHeader()  : void
Ensure header name and values are valid.
filterHeaderValue()  : array<int, string>
getStream()  : StreamInterface
setHeaders()  : void
Filter a set of headers to ensure they are in the correct internal format.
validateProtocolVersion()  : void
Validate the HTTP protocol version

Constants

METHOD_DELETE

HTTP DELETE method

public string METHOD_DELETE = 'DELETE'

METHOD_GET

HTTP GET method

public string METHOD_GET = 'GET'

METHOD_HEAD

HTTP HEAD method

public string METHOD_HEAD = 'HEAD'

METHOD_OPTIONS

HTTP OPTIONS method

public string METHOD_OPTIONS = 'OPTIONS'

METHOD_PATCH

HTTP PATCH method

public string METHOD_PATCH = 'PATCH'

METHOD_POST

HTTP POST method

public string METHOD_POST = 'POST'

METHOD_PUT

HTTP PUT method

public string METHOD_PUT = 'PUT'

METHOD_TRACE

HTTP TRACE method

public string METHOD_TRACE = 'TRACE'

STATUS_ACCEPTED

HTTP 202 code

public int STATUS_ACCEPTED = 202

STATUS_CREATED

HTTP 201 code

public int STATUS_CREATED = 201

STATUS_FOUND

HTTP 302 code

public int STATUS_FOUND = 302

STATUS_MOVED_PERMANENTLY

HTTP 301 code

public int STATUS_MOVED_PERMANENTLY = 301

STATUS_NO_CONTENT

HTTP 204 code

public int STATUS_NO_CONTENT = 204

STATUS_NON_AUTHORITATIVE_INFORMATION

HTTP 203 code

public int STATUS_NON_AUTHORITATIVE_INFORMATION = 203

STATUS_OK

HTTP 200 code

public int STATUS_OK = 200

STATUS_PERMANENT_REDIRECT

HTTP 308 code

public int STATUS_PERMANENT_REDIRECT = 308

STATUS_SEE_OTHER

HTTP 303 code

public int STATUS_SEE_OTHER = 303

STATUS_TEMPORARY_REDIRECT

HTTP 307 code

public int STATUS_TEMPORARY_REDIRECT = 307

Properties

$_cookies

The array of cookies in the response.

protected array<string|int, mixed> $_cookies = []

$_json

Cached decoded JSON data.

protected mixed $_json

$_xml

Cached decoded XML data.

protected SimpleXMLElement $_xml

$code

The status code of the response.

protected int $code

$headerNames

Map of normalized header name to original name used to register header.

protected array<string|int, mixed> $headerNames = []
Tags
psalm-var

array<non-empty-string, non-empty-string>

$headers

List of all registered headers, as key => array of values.

protected array<string|int, mixed> $headers = []
Tags
psalm-var

array<non-empty-string, list>

$reasonPhrase

The reason phrase for the status code

protected string $reasonPhrase

Methods

__construct()

Constructor

public __construct([array<string|int, mixed> $headers = [] ][, string $body = '' ]) : mixed
Parameters
$headers : array<string|int, mixed> = []

Unparsed headers.

$body : string = ''

The response body.

cookies()

Get all cookies

public cookies() : array<string|int, mixed>
Return values
array<string|int, mixed>

getCookie()

Get the value of a single cookie.

public getCookie(string $name) : array<string|int, mixed>|string|null
Parameters
$name : string

The name of the cookie value.

Return values
array<string|int, mixed>|string|null

Either the cookie's value or null when the cookie is undefined.

getCookieCollection()

Get the cookie collection from this response.

public getCookieCollection() : CookieCollection

This method exposes the response's CookieCollection instance allowing you to interact with cookie objects directly.

Return values
CookieCollection

getCookieData()

Get the full data for a single cookie.

public getCookieData(string $name) : array<string|int, mixed>|null
Parameters
$name : string

The name of the cookie value.

Return values
array<string|int, mixed>|null

Either the cookie's data or null when the cookie is undefined.

getCookies()

Get the all cookie data.

public getCookies() : array<string|int, mixed>
Return values
array<string|int, mixed>

The cookie data

getEncoding()

Get the encoding if it was set.

public getEncoding() : string|null
Return values
string|null

getHeader()

Retrieves a message header value by the given case-insensitive name.

public getHeader(string $header) : array<string|int, string>

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
$header : string

Case-insensitive header field name.

Return values
array<string|int, 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()

Retrieves a comma-separated string of the values for a single header.

public getHeaderLine(string $name) : string

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
$name : string

Case-insensitive header field name.

Return values
string

A 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()

Retrieves all message headers.

public getHeaders() : array<string|int, mixed>

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);
    }
}
Tags
psalm-return

array<non-empty-string, list>

Return values
array<string|int, mixed>

Returns 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()

Get the response body as JSON decoded data.

public getJson() : mixed

getProtocolVersion()

Retrieves the HTTP protocol version as a string.

public getProtocolVersion() : string

The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").

Return values
string

HTTP protocol version.

getReasonPhrase()

Gets the response reason phrase associated with the status code.

public getReasonPhrase() : string
Return values
string

The current reason phrase.

getStatusCode()

Gets the response status code.

public getStatusCode() : int
Return values
int

The status code.

getStringBody()

Get the response body as string.

public getStringBody() : string
Return values
string

getXml()

Get the response body as XML decoded data.

public getXml() : SimpleXMLElement|null
Return values
SimpleXMLElement|null

hasHeader()

Checks if a header exists by the given case-insensitive name.

public hasHeader(string $header) : bool
Parameters
$header : string

Case-insensitive header name.

Return values
bool

Returns 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.

isOk()

Check if the response status code was in the 2xx/3xx range

public isOk() : bool
Return values
bool

isRedirect()

Check if the response had a redirect status code.

public isRedirect() : bool
Return values
bool

isSuccess()

Check if the response status code was in the 2xx range

public isSuccess() : bool
Return values
bool

withAddedHeader()

Return an instance with the specified header appended with the given value.

public withAddedHeader(string $name, string|array<string|int, string> $value) : static

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
$name : string

Case-insensitive header field name to add.

$value : string|array<string|int, string>

Header value(s).

Tags
throws
InvalidArgumentException

For invalid header names or values.

Return values
static

withBody()

Return an instance with the specified message body.

public withBody(StreamInterface $body) : static

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
$body : StreamInterface

Body.

Tags
throws
InvalidArgumentException

When the body is not valid.

Return values
static

withHeader()

Return an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name.

public withHeader(string $name, string|array<string|int, string> $value) : static

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
$name : string

Case-insensitive header field name.

$value : string|array<string|int, string>

Header value(s).

Tags
throws
InvalidArgumentException

For invalid header names or values.

Return values
static

withoutHeader()

Return an instance without the specified header.

public withoutHeader(string $name) : static

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
$name : string

Case-insensitive header field name to remove.

Return values
static

withProtocolVersion()

Return an instance with the specified HTTP protocol version.

public withProtocolVersion(string $version) : static

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
$version : string

HTTP protocol version

Return values
static

withStatus()

Return an instance with the specified status code and, optionally, reason phrase.

public withStatus(int $code[, string $reasonPhrase = '' ]) : static
Parameters
$code : int

The status code to set.

$reasonPhrase : string = ''

The status reason phrase.

Return values
static

A copy of the current object with an updated status code.

_decodeGzipBody()

Uncompress a gzip response.

protected _decodeGzipBody(string $body) : string

Looks for gzip signatures, and if gzinflate() exists, the body will be decompressed.

Parameters
$body : string

Gzip encoded body.

Tags
throws
RuntimeException

When attempting to decode gzip content without gzinflate.

Return values
string

_getBody()

Provides magic __get() support.

protected _getBody() : string
Return values
string

_getCookies()

Property accessor for `$this->cookies`

protected _getCookies() : array<string|int, mixed>
Return values
array<string|int, mixed>

Array of Cookie data.

_getHeaders()

Provides magic __get() support.

protected _getHeaders() : array<string|int, string>
Return values
array<string|int, string>

_getJson()

Get the response body as JSON decoded data.

protected _getJson() : mixed

_getXml()

Get the response body as XML decoded data.

protected _getXml() : SimpleXMLElement|null
Return values
SimpleXMLElement|null

_parseHeaders()

Parses headers if necessary.

protected _parseHeaders(array<string|int, mixed> $headers) : void
  • Decodes the status code and reasonphrase.
  • Parses and normalizes header names + values.
Parameters
$headers : array<string|int, mixed>

Headers to parse.

buildCookieCollection()

Lazily build the CookieCollection and cookie objects from the response header

protected buildCookieCollection() : void

filterHeaderValue()

private filterHeaderValue(mixed $values) : array<int, string>
Parameters
$values : mixed
Return values
array<int, string>

setHeaders()

Filter a set of headers to ensure they are in the correct internal format.

private setHeaders(array<string|int, mixed> $originalHeaders) : void

Used by message constructors to allow setting all initial headers at once.

Parameters
$originalHeaders : array<string|int, mixed>

Headers to filter.

validateProtocolVersion()

Validate the HTTP protocol version

private validateProtocolVersion(string $version) : void
Parameters
$version : string
Tags
throws
InvalidArgumentException

On invalid HTTP protocol version.


        
On this page

Search results