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->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'];
Get the response body
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.
Check the status code
You can access the response status code using:
$content = $response->statusCode();
You can also use object access:
$content = $response->code;
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_OK ¶200HTTP 200 code
-
intSTATUS_SEE_OTHER ¶303HTTP 303 code
-
intSTATUS_TEMPORARY_REDIRECT ¶307HTTP 307 code
Property Summary
-
$_body protected
stringThe response body
-
$_code protected
intThe status code of the response.
-
$_cookies protected
arrayThe array of cookies in the response.
-
$_exposedProperties protected
arrayMap of public => property names for __get()
-
$_headers protected
arrayThe array of headers in the response.
-
$_json protected
arrayCached decoded JSON data.
-
$_version protected
stringHTTP Version being used.
-
$_xml protected
SimpleXMLElementCached decoded XML data.
Method Summary
-
__construct() public
Constructor
-
__get() public
Read values as properties.
-
__isset() public
isset/empty test with -> syntax.
-
_decodeGzipBody() protected
Uncompress a gzip response.
-
_getJson() protected
Get the response body as JSON decoded data.
-
_getXml() protected
Get the response body as XML decoded data.
-
_normalizeHeader() protected
Normalize header names to Camel-Case form.
-
_parseCookie() protected
Parse a cookie header into data.
-
_parseHeaders() protected
Parses headers if necessary.
-
body() public
Get the response body.
-
cookie() public
Read single/multiple cookie values out.
-
cookies() public
Get all cookies
-
encoding() public
Get the encoding if it was set.
-
header() public
Read single/multiple header value(s) out.
-
headers() public
Get all headers
-
isOk() public
Check if the response was OK
-
isRedirect() public
Check if the response had a redirect status code.
-
statusCode() public
Get the status code from the response
-
version() public
Get the HTTP version used.
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): mixed
Read values as properties.
Parameters
-
string$name Property name.
Returns
mixed__isset() ¶ public
__isset(string $name): bool
isset/empty test with -> syntax.
Parameters
-
string$name Property name.
Returns
bool_decodeGzipBody() ¶ protected
_decodeGzipBody(string $body): string
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.
_getJson() ¶ protected
_getJson(): null|array
Get the response body as JSON decoded data.
Returns
null|array_getXml() ¶ protected
_getXml(): null|SimpleXMLElement
Get the response body as XML decoded data.
Returns
null|SimpleXMLElement_normalizeHeader() ¶ protected
_normalizeHeader(string $name): string
Normalize header names to Camel-Case form.
Parameters
-
string$name The header name to normalize.
Returns
stringNormalized header name.
_parseCookie() ¶ protected
_parseCookie(string $value): void
Parse a cookie header into data.
Parameters
-
string$value The cookie value to parse.
Returns
void_parseHeaders() ¶ protected
_parseHeaders(array $headers): void
Parses headers if necessary.
- Decodes the status code.
- Parses and normalizes header names + values.
Parameters
-
array$headers Headers to parse.
Returns
voidbody() ¶ public
body(callable|null $parser = null): mixed
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 The callback to use to decode the response body.
Returns
mixedThe response body.
cookie() ¶ public
cookie(string|null $name = null, bool $all = false): mixed
Read single/multiple cookie values out.
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
mixedheader() ¶ public
header(string|null $name = null): mixed
Read 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(): bool
Check if the response had a redirect status code.
Returns
bool