class Response extends Message (View source)

The http\Client\Response class represents an HTTP message the client returns as answer from a server to an http\Client\Request.

Constants

TYPE_NONE

No specific type of message.

TYPE_REQUEST

A request message.

TYPE_RESPONSE

A response message.

Properties

protected int $type

The message type. See http\Message::TYPE_* constants.

from  Message
protected Body $body

The message's body.

from  Message
protected string $requestMethod

The request method if the message is of type request.

from  Message
protected string $requestUrl

The request url if the message is of type request.

from  Message
protected string $responseStatus

The response status phrase if the message is of type response.

from  Message
protected int $responseCode

The response code if the message is of type response.

from  Message
protected string $httpVersion

A custom HTTP protocol version.

from  Message
protected array $headers

Any message headers.

from  Message
protected Message $parentMessage

Any parent message.

from  Message

Methods

__construct(mixed $message = null, bool $greedy = true)

Create a new HTTP message.

from  Message
string
__toString()

Retrieve the message serialized to a string.

from  Message
addBody(Body $body)

Append the data of $body to the message's body.

from  Message
addHeader(string $name, mixed $value)

Add an header, appending to already existing headers.

from  Message
addHeaders(array $headers, bool $append = false)

Add headers, optionally appending values, if header keys already exist.

from  Message
int
count()

Implements Countable.

from  Message
mixed
current()

Implements iterator.

from  Message
detach()

Detach a clone of this message from any message chain.

from  Message
getBody()

Retrieve the message's body.

from  Message
mixed|Header
getHeader(string $header, string $into_class = null)

Retrieve a single header, optionally hydrated into a http\Header extending class.

from  Message
array
getHeaders()

Retrieve all message headers.

from  Message
string
getHttpVersion()

Retrieve the HTTP protocol version of the message.

from  Message
string|null
getInfo()

Retrieve the first line of a request or response message.

from  Message
getParentMessage()

Retrieve any parent message.

from  Message
string|false
getRequestMethod()

Retrieve the request method of the message.

from  Message
string|false
getRequestUrl()

Retrieve the request URL of the message.

from  Message
int|false
getResponseCode()

Retrieve the response code of the message.

from  Message
string|false
getResponseStatus()

Retrieve the response status of the message.

from  Message
int
getType()

Retrieve the type of the message.

from  Message
bool
isMultipart(string $boundary = null)

Check whether this message is a multipart message based on it's content type.

from  Message
mixed
key()

Implements Iterator.

from  Message
void
next()

Implements Iterator.

from  Message
prepend(Message $message, bool $top = true)

Prepend message(s) $message to this message, or the top most message of this message chain.

from  Message
reverse()

Reverse the message chain and return the former top-most message.

from  Message
void
rewind()

Implements Iterator.

from  Message
string|null
serialize()

Implements Serializable.

from  Message
setBody(Body $body)

Set the message's body.

from  Message
setHeader(string $header, mixed $value = null)

Set a single header.

from  Message
setHeaders(array $headers = null)

Set the message headers.

from  Message
setHttpVersion(string $http_version)

Set the HTTP protocol version of the message.

from  Message
setInfo(string $http_info)

Set the complete message info, i.e. type and response resp. request information, at once.

from  Message
setRequestMethod(string $method)

Set the request method of the message.

from  Message
setRequestUrl(string $url)

Set the request URL of the message.

from  Message
setResponseCode(int $response_code, bool $strict = true)

Set the response status code.

from  Message
setResponseStatus(string $response_status)

Set the response status phrase.

from  Message
setType(int $type)

Set the message type and reset the message info.

from  Message
splitMultipartBody()

Splits the body of a multipart message.

from  Message
toCallback(callable $callback)

Stream the message through a callback.

from  Message
toStream(resource $stream)

Stream the message into stream $stream, starting from $offset, streaming $maxlen at most.

from  Message
string
toString(bool $include_parent = false)

Retrieve the message serialized to a string.

from  Message
void
unserialize(string $data)

Implements Serializable.

from  Message
bool
valid()

Implements Iterator.

from  Message
array
getCookies(int $flags = 0, array $allowed_extras = null)

Extract response cookies.

object|mixed
getTransferInfo(string $name = null)

Retrieve transfer related information after the request has completed.

Details

__construct(mixed $message = null, bool $greedy = true)

Create a new HTTP message.

Parameters

mixed $message

Either a resource or a string, representing the HTTP message.

bool $greedy

Whether to read from a $message resource until EOF.

Exceptions

InvalidArgumentException
BadMessageException

string __toString()

Retrieve the message serialized to a string.

Alias of http\Message::toString().

Return Value

string

the single serialized HTTP message.

Message addBody(Body $body)

Append the data of $body to the message's body.

See http\Message::setBody() and http\Message\Body::append().

Parameters

Body $body

The message body to add.

Return Value

Message self.

Message addHeader(string $name, mixed $value)

Add an header, appending to already existing headers.

See http\Message::addHeaders() and http\Message::setHeader().

Parameters

string $name

The header name.

mixed $value

The header value.

Return Value

Message self.

Message addHeaders(array $headers, bool $append = false)

Add headers, optionally appending values, if header keys already exist.

See http\Message::addHeader() and http\Message::setHeaders().

Parameters

array $headers

The HTTP headers to add.

bool $append

Whether to append values for existing headers.

Return Value

Message self.

int count()

Implements Countable.

Return Value

int

The custom count as an integer.

The return value is cast to an integer.

mixed current()

Implements iterator.

See http\Message::valid() and http\Message::rewind().

Return Value

mixed

Can return any type.

Message detach()

Detach a clone of this message from any message chain.

Return Value

Message clone.

Exceptions

InvalidArgumentException

Body getBody()

Retrieve the message's body.

See http\Message::setBody().

Return Value

Body

the message body.

Exceptions

InvalidArgumentException
UnexpectedValueException

mixed|Header getHeader(string $header, string $into_class = null)

Retrieve a single header, optionally hydrated into a http\Header extending class.

Parameters

string $header

The header's name.

string $into_class

The name of a class extending http\Header.

Return Value

mixed|Header

mixed the header value if $into_class is NULL. or \http\Header descendant.

array getHeaders()

Retrieve all message headers.

See http\Message::setHeaders() and http\Message::getHeader().

Return Value

array

the message's headers.

string getHttpVersion()

Retrieve the HTTP protocol version of the message.

See http\Message::setHttpVersion().

Return Value

string

the HTTP protocol version, e.g. "1.0"; defaults to "1.1".

string|null getInfo()

Retrieve the first line of a request or response message.

See http\Message::setInfo and also:

  • http\Message::getType()
  • http\Message::getHttpVersion()
  • http\Message::getResponseCode()
  • http\Message::getResponseStatus()
  • http\Message::getRequestMethod()
  • http\Message::getRequestUrl()

Return Value

string|null

string the HTTP message information. or NULL if the message is neither of type request nor response.

Message getParentMessage()

Retrieve any parent message.

See http\Message::reverse().

Return Value

Message

the parent message.

Exceptions

InvalidArgumentException
BadMethodCallException

string|false getRequestMethod()

Retrieve the request method of the message.

See http\Message::setRequestMethod() and http\Message::getRequestUrl().

Return Value

string|false

string the request method. or false if the message was not of type request.

string|false getRequestUrl()

Retrieve the request URL of the message.

See http\Message::setRequestUrl().

Return Value

string|false

string the request URL; usually the path and the querystring. or false if the message was not of type request.

int|false getResponseCode()

Retrieve the response code of the message.

See http\Message::setResponseCode() and http\Message::getResponseStatus().

Return Value

int|false

int the response status code. or false if the message is not of type response.

string|false getResponseStatus()

Retrieve the response status of the message.

See http\Message::setResponseStatus() and http\Message::getResponseCode().

Return Value

string|false

string the response status phrase. or false if the message is not of type response.

int getType()

Retrieve the type of the message.

See http\Message::setType() and http\Message::getInfo().

Return Value

int

the message type. See http\Message::TYPE_* constants.

bool isMultipart(string $boundary = null)

Check whether this message is a multipart message based on it's content type.

If the message is a multipart message and a reference $boundary is given, the boundary string of the multipart message will be stored in $boundary.

See http\Message::splitMultipartBody().

Parameters

string $boundary

A reference where the boundary string will be stored.

Return Value

bool

whether this is a message with a multipart "Content-Type".

mixed key()

Implements Iterator.

See http\Message::current() and http\Message::rewind().

Return Value

mixed

TKey on success, or null on failure.

void next()

Implements Iterator.

See http\Message::valid() and http\Message::rewind().

Return Value

void

Any returned value is ignored.

Message prepend(Message $message, bool $top = true)

Prepend message(s) $message to this message, or the top most message of this message chain.

NOTE: The message chains must not overlap.

Parameters

Message $message

The message (chain) to prepend as parent messages.

bool $top

Whether to prepend to the top-most parent message.

Return Value

Message self.

Exceptions

InvalidArgumentException
UnexpectedValueException

Message reverse()

Reverse the message chain and return the former top-most message.

NOTE: Message chains are ordered in reverse-parsed order by default, i.e. the last parsed message is the message you'll receive from any call parsing HTTP messages.

This call re-orders the messages of the chain and returns the message that was parsed first with any later parsed messages re-parentized.

Return Value

Message

the other end of the message chain.

Exceptions

InvalidArgumentException

void rewind()

Implements Iterator.

Return Value

void

Any returned value is ignored.

string|null serialize()

Implements Serializable.

Return Value

string|null

The string representation of the object or null

Message setBody(Body $body)

Set the message's body.

See http\Message::getBody() and http\Message::addBody().

Parameters

Body $body

The new message body.

Return Value

Message self.

Exceptions

InvalidArgumentException
UnexpectedValueException

Message setHeader(string $header, mixed $value = null)

Set a single header.

See http\Message::getHeader() and http\Message::addHeader().

NOTE: Prior to v2.5.6/v3.1.0 headers with the same name were merged into a single header with values concatenated by comma.

Parameters

string $header

The header's name.

mixed $value

The header's value. Removes the header if NULL.

Return Value

Message self.

Message setHeaders(array $headers = null)

Set the message headers.

See http\Message::getHeaders() and http\Message::addHeaders().

NOTE: Prior to v2.5.6/v3.1.0 headers with the same name were merged into a single header with values concatenated by comma.

Parameters

array $headers

The message's headers.

Return Value

Message null.

Message setHttpVersion(string $http_version)

Set the HTTP protocol version of the message.

See http\Message::getHttpVersion().

Parameters

string $http_version

The protocol version, e.g. "1.1", optionally prefixed by "HTTP/".

Return Value

Message self.

Exceptions

InvalidArgumentException
BadHeaderException

Message setInfo(string $http_info)

Set the complete message info, i.e. type and response resp. request information, at once.

See http\Message::getInfo().

Parameters

string $http_info

The message info (first line of an HTTP message).

Return Value

Message self.

Exceptions

InvalidArgumentException
BadHeaderException

Message setRequestMethod(string $method)

Set the request method of the message.

See http\Message::getRequestMethod() and http\Message::setRequestUrl().

Parameters

string $method

The request method.

Return Value

Message self.

Exceptions

InvalidArgumentException
BadMethodCallException

Message setRequestUrl(string $url)

Set the request URL of the message.

See http\Message::getRequestUrl() and http\Message::setRequestMethod().

Parameters

string $url

The request URL.

Return Value

Message self.

Exceptions

InvalidArgumentException
BadMethodCallException

Message setResponseCode(int $response_code, bool $strict = true)

Set the response status code.

See http\Message::getResponseCode() and http\Message::setResponseStatus().

NOTE: This method also resets the response status phrase to the default for that code.

Parameters

int $response_code

The response code.

bool $strict

Whether to check that the response code is between 100 and 599 inclusive.

Return Value

Message self.

Exceptions

InvalidArgumentException
BadMethodCallException

Message setResponseStatus(string $response_status)

Set the response status phrase.

See http\Message::getResponseStatus() and http\Message::setResponseCode().

Parameters

string $response_status

The status phrase.

Return Value

Message self.

Exceptions

InvalidArgumentException
BadMethodCallException

Message setType(int $type)

Set the message type and reset the message info.

See http\Message::getType() and http\Message::setInfo().

Parameters

int $type

The desired message type. See the http\Message::TYPE_* constants.

Return Value

Message self.

Message splitMultipartBody()

Splits the body of a multipart message.

See http\Message::isMultipart() and http\Message\Body::addPart().

Return Value

Message

a message chain of all messages of the multipart body.

Exceptions

InvalidArgumentException
BadMethodCallException
BadMessageException

Message toCallback(callable $callback)

Stream the message through a callback.

Parameters

callable $callback

The callback of the form function(http\Message $from, string $data).

Return Value

Message self.

Message toStream(resource $stream)

Stream the message into stream $stream, starting from $offset, streaming $maxlen at most.

Parameters

resource $stream

The resource to write to.

Return Value

Message self.

string toString(bool $include_parent = false)

Retrieve the message serialized to a string.

Parameters

bool $include_parent

Whether to include all parent messages.

Return Value

string

the HTTP message chain serialized to a string.

void unserialize(string $data)

Implements Serializable.

Parameters

string $data

The string representation of the object.

Return Value

void

bool valid()

Implements Iterator.

See http\Message::current() and http\Message::rewind().

Return Value

bool

The return value will be casted to boolean and then evaluated. Returns true on success or false on failure.

array getCookies(int $flags = 0, array $allowed_extras = null)

Extract response cookies.

Parses any "Set-Cookie" response headers into an http\Cookie list. See http\Cookie::__construct().

Parameters

int $flags

Cookie parser flags.

array $allowed_extras

List of keys treated as extras.

Return Value

array

list of http\Cookie instances.

object|mixed getTransferInfo(string $name = null)

Retrieve transfer related information after the request has completed.

See http\Client::getTransferInfo().

Parameters

string $name

A key to retrieve out of the transfer info.

Return Value

object|mixed

object stdClass instance with all transfer info if $name was not given. or mixed the specific transfer info for $name.

Exceptions

InvalidArgumentException
BadMethodCallException
UnexpectedValueException