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
-
string
METHOD_DELETE ¶'DELETE'
HTTP DELETE method
-
string
METHOD_GET ¶'GET'
HTTP GET method
-
string
METHOD_HEAD ¶'HEAD'
HTTP HEAD method
-
string
METHOD_OPTIONS ¶'OPTIONS'
HTTP OPTIONS method
-
string
METHOD_PATCH ¶'PATCH'
HTTP PATCH method
-
string
METHOD_POST ¶'POST'
HTTP POST method
-
string
METHOD_PUT ¶'PUT'
HTTP PUT method
-
string
METHOD_TRACE ¶'TRACE'
HTTP TRACE method
-
int
STATUS_ACCEPTED ¶202
HTTP 202 code
-
int
STATUS_CREATED ¶201
HTTP 201 code
-
int
STATUS_FOUND ¶302
HTTP 302 code
-
int
STATUS_MOVED_PERMANENTLY ¶301
HTTP 301 code
-
int
STATUS_OK ¶200
HTTP 200 code
-
int
STATUS_SEE_OTHER ¶303
HTTP 303 code
-
int
STATUS_TEMPORARY_REDIRECT ¶307
HTTP 307 code
Property Summary
-
$_body protected
string
The response body
-
$_code protected
int
The status code of the response.
-
$_cookies protected
array
The array of cookies in the response.
-
$_exposedProperties protected
array
Map of public => property names for __get()
-
$_headers protected
array
The array of headers in the response.
-
$_json protected
array
Cached decoded JSON data.
-
$_version protected
string
HTTP Version being used.
-
$_xml protected
SimpleXMLElement
Cached 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
string
Throws
RuntimeException
When 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(): nullSimpleXMLElement
Get the response body as XML decoded data.
Returns
nullSimpleXMLElement
_normalizeHeader() ¶ protected
_normalizeHeader(string $name): string
Normalize header names to Camel-Case form.
Parameters
-
string
$name The header name to normalize.
Returns
string
_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
void
body() ¶ 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
mixed
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
mixed
header() ¶ 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
mixed
isRedirect() ¶ public
isRedirect(): bool
Check if the response had a redirect status code.
Returns
bool