Documentation

Response extends AbstractMessage
in package
implements Serializable

Guzzle HTTP response object

Table of Contents

Interfaces

Serializable

Properties

$body  : EntityBodyInterface
$cacheResponseCodes  : array<string|int, mixed>
$effectiveUrl  : string
$headerFactory  : HeaderFactoryInterface
$headers  : array<string|int, mixed>
$info  : array<string|int, mixed>
$params  : Collection
$protocol  : string
$protocolVersion  : string
$reasonPhrase  : string
$statusCode  : string
$statusTexts  : array<string|int, mixed>

Methods

__construct()  : mixed
Construct the response
__serialize()  : mixed
__toString()  : string
__unserialize()  : mixed
addCacheControlDirective()  : mixed
addHeader()  : self
Add a header to an existing collection of headers.
addHeaders()  : self
Add and merge in an array of HTTP headers.
calculateAge()  : int
Calculate the age of the response
canCache()  : bool
Check if the response can be cached based on the response headers
canValidate()  : bool
Check if the response can be validated against the origin server using a conditional GET request.
fromMessage()  : self|bool
Create a new Response based on a raw response message
getAcceptRanges()  : string
Get the Accept-Ranges HTTP header
getAge()  : int|null
Get the Age HTTP header
getAllow()  : string|null
Get the Allow HTTP header
getBody()  : EntityBodyInterface|string
Get the response entity body
getCacheControl()  : string
Get the Cache-Control HTTP header
getCacheControlDirective()  : mixed
getConnection()  : string
Get the Connection HTTP header
getContentDisposition()  : string|null
Get the Content-Disposition HTTP header
getContentEncoding()  : string|null
Get the Content-Encoding HTTP header
getContentLanguage()  : string|null
Get the Content-Language HTTP header
getContentLength()  : int
Get the Content-Length HTTP header
getContentLocation()  : string|null
Get the Content-Location HTTP header
getContentMd5()  : string|null
Get the Content-MD5 HTTP header
getContentRange()  : string
Get the Content-Range HTTP header
getContentType()  : string
Get the Content-Type HTTP header
getDate()  : string|null
Get the Date HTTP header
getEffectiveUrl()  : string
Get the effective URL that resulted in this response (e.g. the last redirect URL)
getEtag()  : string|null
Get the ETag HTTP header
getExpires()  : string|null
Get the Expires HTTP header
getFreshness()  : int
Get the freshness of the response by returning the difference of the maximum lifetime of the response and the age of the response (max-age - age).
getHeader()  : Header|null
Retrieve an HTTP header by name. Performs a case-insensitive search of all headers.
getHeaderLines()  : array<string|int, mixed>
Get an array of message header lines (e.g. ["Host: example.com", ...])
getHeaders()  : HeaderCollection
Get all headers as a collection
getInfo()  : array<string|int, mixed>|string|null
Get a cURL transfer information
getLastModified()  : string|null
Get the Last-Modified HTTP header
getLocation()  : string|null
Get the Location HTTP header
getMaxAge()  : int|null
Gets the number of seconds from the current time in which this response is still considered fresh
getMessage()  : string
Get the entire response as a string
getParams()  : Collection
Get application and plugin specific parameters set on the message.
getPragma()  : Header|null
Get the Pragma HTTP header
getPreviousResponse()  : mixed
getProtocol()  : string
Get the protocol used for the response (e.g. HTTP)
getProtocolVersion()  : string
Get the HTTP protocol version
getProxyAuthenticate()  : string|null
Get the Proxy-Authenticate HTTP header
getRawHeaders()  : string
Get the the raw message headers as a string
getReasonPhrase()  : string
Get the response reason phrase- a human readable version of the numeric status code
getRedirectCount()  : int
Get the redirect count of this response
getRequest()  : mixed
getRetryAfter()  : int|null
Get the Retry-After HTTP header
getServer()  : string|null
Get the Server HTTP header
getSetCookie()  : string|null
Get the Set-Cookie HTTP header
getStatusCode()  : int
Get the response status code
getTokenizedHeader()  : mixed
getTrailer()  : string|null
Get the Trailer HTTP header
getTransferEncoding()  : string|null
Get the Transfer-Encoding HTTP header
getVary()  : string|null
Get the Vary HTTP header
getVia()  : string|null
Get the Via HTTP header
getWarning()  : string|null
Get the Warning HTTP header
getWwwAuthenticate()  : string|null
Get the WWW-Authenticate HTTP header
hasCacheControlDirective()  : mixed
hasHeader()  : bool
Check if the specified header is present.
isClientError()  : bool
Checks if HTTP Status code is a Client Error (4xx)
isContentType()  : bool
Checks if the Content-Type is of a certain type. This is useful if the Content-Type header contains charset information and you need to know if the Content-Type matches a particular type.
isError()  : bool
Checks if HTTP Status code is Server OR Client Error (4xx or 5xx)
isFresh()  : bool|null
Check if the response is considered fresh.
isInformational()  : bool
Checks if HTTP Status code is Information (1xx)
isMethodAllowed()  : bool
Check if an HTTP method is allowed by checking the Allow response header
isRedirect()  : bool
Checks if HTTP Status code is a Redirect (3xx)
isServerError()  : bool
Checks if HTTP Status code is Server Error (5xx)
isSuccessful()  : bool
Checks if HTTP Status code is Successful (2xx | 304)
json()  : array<string|int, mixed>|string|int|bool|float
Parse the JSON response body and return an array
removeCacheControlDirective()  : mixed
removeHeader()  : self
Remove a specific HTTP header.
serialize()  : mixed
setBody()  : self
Set the response entity body
setEffectiveUrl()  : self
Set the effective URL that resulted in this response (e.g. the last redirect URL)
setHeader()  : self
Set an HTTP header and overwrite any existing value for the header
setHeaderFactory()  : self
Set the header factory to use to create headers
setHeaders()  : self
Overwrite all HTTP headers with the supplied array of headers
setInfo()  : self
Set the transfer information
setProtocol()  : self
Set the protocol and protocol version of the response
setRequest()  : mixed
setStatus()  : self
Set the response status
setTokenizedHeader()  : mixed
unserialize()  : mixed
xml()  : SimpleXMLElement
Parse the XML response body and return a \SimpleXMLElement.

Properties

$cacheResponseCodes

protected static array<string|int, mixed> $cacheResponseCodes = array(200, 203, 206, 300, 301, 410)

Cacheable response codes (see RFC 2616:13.4)

$effectiveUrl

protected string $effectiveUrl

The effective URL that returned this response

$headers

protected array<string|int, mixed> $headers

HTTP header collection

$info

protected array<string|int, mixed> $info = array()

Information about the request

$protocolVersion

protected string $protocolVersion = '1.1'

HTTP protocol version of the message

$reasonPhrase

protected string $reasonPhrase

The reason phrase of the response (human readable code)

$statusCode

protected string $statusCode

The status code of the response

$statusTexts

private static array<string|int, mixed> $statusTexts = array(100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-Status', 208 => 'Already Reported', 226 => 'IM Used', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', 308 => 'Permanent Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 425 => 'Reserved for WebDAV advanced collections expired proposal', 426 => 'Upgrade required', 428 => 'Precondition Required', 429 => 'Too Many Requests', 431 => 'Request Header Fields Too Large', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 506 => 'Variant Also Negotiates (Experimental)', 507 => 'Insufficient Storage', 508 => 'Loop Detected', 510 => 'Not Extended', 511 => 'Network Authentication Required')

Array of reason phrases and their corresponding status codes

Methods

__construct()

Construct the response

public __construct(string $statusCode[, ToArrayInterface|array<string|int, mixed> $headers = null ][, string|resource|EntityBodyInterface $body = null ]) : mixed
Parameters
$statusCode : string

The response status code (e.g. 200, 404, etc)

$headers : ToArrayInterface|array<string|int, mixed> = null

The response headers

$body : string|resource|EntityBodyInterface = null

The body of the response

Tags
throws
BadResponseException

if an invalid response code is given

__serialize()

public __serialize() : mixed

__toString()

public __toString() : string
Return values
string

__unserialize()

public __unserialize(mixed $serialize) : mixed
Parameters
$serialize : mixed

addCacheControlDirective()

public addCacheControlDirective(mixed $directive[, mixed $value = true ]) : mixed
Parameters
$directive : mixed
$value : mixed = true
Tags
codeCoverageIgnore

addHeader()

Add a header to an existing collection of headers.

public addHeader(mixed $header, mixed $value) : self
Parameters
$header : mixed

Header name to add

$value : mixed

Value of the header

Return values
self

addHeaders()

Add and merge in an array of HTTP headers.

public addHeaders(array<string|int, mixed> $headers) : self
Parameters
$headers : array<string|int, mixed>

Associative array of header data.

Return values
self

calculateAge()

Calculate the age of the response

public calculateAge() : int
Return values
int

canCache()

Check if the response can be cached based on the response headers

public canCache() : bool
Return values
bool

Returns TRUE if the response can be cached or false if not

canValidate()

Check if the response can be validated against the origin server using a conditional GET request.

public canValidate() : bool
Return values
bool

fromMessage()

Create a new Response based on a raw response message

public static fromMessage(string $message) : self|bool
Parameters
$message : string

Response message

Return values
self|bool

Returns false on error

getAcceptRanges()

Get the Accept-Ranges HTTP header

public getAcceptRanges() : string
Return values
string

Returns what partial content range types this server supports.

getAge()

Get the Age HTTP header

public getAge() : int|null
Return values
int|null

Returns the age the object has been in a proxy cache in seconds.

getAllow()

Get the Allow HTTP header

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

Returns valid actions for a specified resource. To be used for a 405 Method not allowed.

getBody()

Get the response entity body

public getBody([bool $asString = false ]) : EntityBodyInterface|string
Parameters
$asString : bool = false

Set to TRUE to return a string of the body rather than a full body object

Return values
EntityBodyInterface|string

getCacheControl()

Get the Cache-Control HTTP header

public getCacheControl() : string
Return values
string

getCacheControlDirective()

public getCacheControlDirective(mixed $directive) : mixed
Parameters
$directive : mixed
Tags
codeCoverageIgnore

getConnection()

Get the Connection HTTP header

public getConnection() : string
Return values
string

getContentDisposition()

Get the Content-Disposition HTTP header

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

Returns the Content-Disposition header

getContentEncoding()

Get the Content-Encoding HTTP header

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

getContentLanguage()

Get the Content-Language HTTP header

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

Returns the language the content is in.

getContentLength()

Get the Content-Length HTTP header

public getContentLength() : int
Return values
int

Returns the length of the response body in bytes

getContentLocation()

Get the Content-Location HTTP header

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

Returns an alternate location for the returned data (e.g /index.htm)

getContentMd5()

Get the Content-MD5 HTTP header

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

Returns a Base64-encoded binary MD5 sum of the content of the response.

getContentRange()

Get the Content-Range HTTP header

public getContentRange() : string
Return values
string

Returns where in a full body message this partial message belongs (e.g. bytes 21010-47021/47022).

getContentType()

Get the Content-Type HTTP header

public getContentType() : string
Return values
string

Returns the mime type of this content.

getDate()

Get the Date HTTP header

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

Returns the date and time that the message was sent.

getEffectiveUrl()

Get the effective URL that resulted in this response (e.g. the last redirect URL)

public getEffectiveUrl() : string
Return values
string

getEtag()

Get the ETag HTTP header

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

Returns an identifier for a specific version of a resource, often a Message digest.

getExpires()

Get the Expires HTTP header

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

Returns the date/time after which the response is considered stale.

getFreshness()

Get the freshness of the response by returning the difference of the maximum lifetime of the response and the age of the response (max-age - age).

public getFreshness() : int

Freshness values less than 0 mean that the response is no longer fresh and is ABS(freshness) seconds expired. Freshness values of greater than zero is the number of seconds until the response is no longer fresh. A NULL result means that no freshness information is available.

Return values
int

getHeader()

Retrieve an HTTP header by name. Performs a case-insensitive search of all headers.

public getHeader(mixed $header) : Header|null
Parameters
$header : mixed

Header to retrieve.

Return values
Header|null

getHeaderLines()

Get an array of message header lines (e.g. ["Host: example.com", ...])

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

getInfo()

Get a cURL transfer information

public getInfo([string $key = null ]) : array<string|int, mixed>|string|null
Parameters
$key : string = null

A single statistic to check

Tags
link
http://www.php.net/manual/en/function.curl-getinfo.php
Return values
array<string|int, mixed>|string|null

Returns all stats if no key is set, a single stat if a key is set, or null if a key is set and not found

getLastModified()

Get the Last-Modified HTTP header

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

Returns the last modified date for the requested object, in RFC 2822 format (e.g. Tue, 15 Nov 1994 12:45:26 GMT)

getLocation()

Get the Location HTTP header

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

Used in redirection, or when a new resource has been created.

getMaxAge()

Gets the number of seconds from the current time in which this response is still considered fresh

public getMaxAge() : int|null
Return values
int|null

Returns the number of seconds

getMessage()

Get the entire response as a string

public getMessage() : string
Return values
string

getPragma()

Get the Pragma HTTP header

public getPragma() : Header|null
Return values
Header|null

Returns the implementation-specific headers that may have various effects anywhere along the request-response chain.

getPreviousResponse()

public getPreviousResponse() : mixed
Tags
codeCoverageIgnore

getProtocol()

Get the protocol used for the response (e.g. HTTP)

public getProtocol() : string
Return values
string

getProtocolVersion()

Get the HTTP protocol version

public getProtocolVersion() : string
Return values
string

getProxyAuthenticate()

Get the Proxy-Authenticate HTTP header

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

Authentication to access the proxy (e.g. Basic)

getRawHeaders()

Get the the raw message headers as a string

public getRawHeaders() : string
Return values
string

getReasonPhrase()

Get the response reason phrase- a human readable version of the numeric status code

public getReasonPhrase() : string
Return values
string

getRedirectCount()

Get the redirect count of this response

public getRedirectCount() : int
Return values
int

getRequest()

public getRequest() : mixed
Tags
codeCoverageIgnore

getRetryAfter()

Get the Retry-After HTTP header

public getRetryAfter() : int|null
Return values
int|null

If an entity is temporarily unavailable, this instructs the client to try again after a specified period of time.

getServer()

Get the Server HTTP header

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

A name for the server

getSetCookie()

Get the Set-Cookie HTTP header

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

An HTTP cookie.

getStatusCode()

Get the response status code

public getStatusCode() : int
Return values
int

getTokenizedHeader()

public getTokenizedHeader(mixed $header[, mixed $token = ';' ]) : mixed

Use $message->getHeader()->parseParams()

Parameters
$header : mixed
$token : mixed = ';'
Tags
codeCoverageIgnore

getTrailer()

Get the Trailer HTTP header

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

The Trailer general field value indicates that the given set of header fields is present in the trailer of a message encoded with chunked transfer-coding.

getTransferEncoding()

Get the Transfer-Encoding HTTP header

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

The form of encoding used to safely transfer the entity to the user

getVary()

Get the Vary HTTP header

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

Tells downstream proxies how to match future request headers to decide whether the cached response can be used rather than requesting a fresh one from the origin server.

getVia()

Get the Via HTTP header

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

Informs the client of proxies through which the response was sent.

getWarning()

Get the Warning HTTP header

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

A general warning about possible problems with the entity body

getWwwAuthenticate()

Get the WWW-Authenticate HTTP header

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

Indicates the authentication scheme that should be used to access the requested entity

hasCacheControlDirective()

public hasCacheControlDirective(mixed $directive) : mixed
Parameters
$directive : mixed
Tags
codeCoverageIgnore

hasHeader()

Check if the specified header is present.

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

The header to check.

Return values
bool

isClientError()

Checks if HTTP Status code is a Client Error (4xx)

public isClientError() : bool
Return values
bool

isContentType()

Checks if the Content-Type is of a certain type. This is useful if the Content-Type header contains charset information and you need to know if the Content-Type matches a particular type.

public isContentType(string $type) : bool
Parameters
$type : string

Content type to check against

Return values
bool

isError()

Checks if HTTP Status code is Server OR Client Error (4xx or 5xx)

public isError() : bool
Return values
bool

isFresh()

Check if the response is considered fresh.

public isFresh() : bool|null

A response is considered fresh when its age is less than or equal to the freshness lifetime (maximum age) of the response.

Return values
bool|null

isInformational()

Checks if HTTP Status code is Information (1xx)

public isInformational() : bool
Return values
bool

isMethodAllowed()

Check if an HTTP method is allowed by checking the Allow response header

public isMethodAllowed(string $method) : bool
Parameters
$method : string

Method to check

Return values
bool

isRedirect()

Checks if HTTP Status code is a Redirect (3xx)

public isRedirect() : bool
Return values
bool

isServerError()

Checks if HTTP Status code is Server Error (5xx)

public isServerError() : bool
Return values
bool

isSuccessful()

Checks if HTTP Status code is Successful (2xx | 304)

public isSuccessful() : bool
Return values
bool

json()

Parse the JSON response body and return an array

public json() : array<string|int, mixed>|string|int|bool|float
Tags
throws
RuntimeException

if the response body is not in JSON format

Return values
array<string|int, mixed>|string|int|bool|float

removeCacheControlDirective()

public removeCacheControlDirective(mixed $directive) : mixed
Parameters
$directive : mixed
Tags
codeCoverageIgnore

removeHeader()

Remove a specific HTTP header.

public removeHeader(mixed $header) : self
Parameters
$header : mixed

HTTP header to remove.

Return values
self

setEffectiveUrl()

Set the effective URL that resulted in this response (e.g. the last redirect URL)

public setEffectiveUrl(string $url) : self
Parameters
$url : string

The effective URL

Return values
self

setHeader()

Set an HTTP header and overwrite any existing value for the header

public setHeader(mixed $header, mixed $value) : self
Parameters
$header : mixed

Name of the header to set.

$value : mixed

Value to set.

Return values
self

setHeaders()

Overwrite all HTTP headers with the supplied array of headers

public setHeaders(array<string|int, mixed> $headers) : self
Parameters
$headers : array<string|int, mixed>

Associative array of header data.

Return values
self

setInfo()

Set the transfer information

public setInfo(array<string|int, mixed> $info) : self
Parameters
$info : array<string|int, mixed>

Array of cURL transfer stats

Return values
self

setProtocol()

Set the protocol and protocol version of the response

public setProtocol(string $protocol, string $version) : self
Parameters
$protocol : string

Response protocol

$version : string

Protocol version

Return values
self

setRequest()

public setRequest(mixed $request) : mixed
Parameters
$request : mixed
Tags
codeCoverageIgnore

setStatus()

Set the response status

public setStatus(int $statusCode[, string $reasonPhrase = '' ]) : self
Parameters
$statusCode : int

Response status code to set

$reasonPhrase : string = ''

Response reason phrase

Tags
throws
BadResponseException

when an invalid response code is received

Return values
self

setTokenizedHeader()

public setTokenizedHeader(mixed $header, mixed $data[, mixed $token = ';' ]) : mixed
Parameters
$header : mixed
$data : mixed
$token : mixed = ';'
Tags
codeCoverageIgnore

unserialize()

public unserialize(mixed $serialize) : mixed
Parameters
$serialize : mixed

xml()

Parse the XML response body and return a \SimpleXMLElement.

public xml() : SimpleXMLElement

In order to prevent XXE attacks, this method disables loading external entities. If you rely on external entities, then you must parse the XML response manually by accessing the response body directly.

Tags
throws
RuntimeException

if the response body is not in XML format

link
http://websec.io/2012/08/27/Preventing-XXE-in-PHP.html
Return values
SimpleXMLElement

        
On this page

Search results