class Response extends Message (View source)

The http\Env\Response class' instances represent the server's current HTTP response.

See http\Message for inherited members.

Constants

TYPE_NONE

No specific type of message.

TYPE_REQUEST

A request message.

TYPE_RESPONSE

A response message.

CONTENT_ENCODING_NONE

Do not use content encoding.

CONTENT_ENCODING_GZIP

Support "Accept-Encoding" requests with gzip and deflate encoding.

CACHE_NO

No caching info available.

CACHE_HIT

The cache was hit.

CACHE_MISS

The cache was missed.

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
protected Request $request

A request instance which overrides the environments default request.

protected string $contentType

The response's MIME content type.

protected string $contentDisposition

The response's MIME content disposition.

protected int $contentEncoding

See http\Env\Response::CONTENTENCODING* constants.

protected string $cacheControl

How the client should treat this response in regards to caching.

protected string $etag

A custom ETag.

protected int $lastModified

A "Last-Modified" time stamp.

protected int $throttleDelay

Any throttling delay.

protected int $throttleChunk

The chunk to send every $throttleDelay seconds.

protected array $cookies

The response's cookies.

Methods

__construct()

Create a new env response message instance.

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<0, max>
count()

Implements Countable.

from  Message
TValue
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|null $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
TKey|null
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|null $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
bool
__invoke(string $data, int $ob_flags = 0)

Output buffer handler.

int
isCachedByEtag(string $header_name = "If-None-Match")

Manually test the header $header_name of the environment's request for a cache hit.

int
isCachedByLastModified(string $header_name = "If-Modified-Since")

Manually test the header $header_name of the environment's request for a cache hit.

bool
send(resource $stream = null)

Send the response through the SAPI or $stream.

setCacheControl(string $cache_control)

Make suggestions to the client how it should cache the response.

setContentDisposition(array $disposition_params)

Set the response's content disposition parameters.

setContentEncoding(int $content_encoding)

Enable support for "Accept-Encoding" requests with deflate or gzip.

setContentType(string $content_type)

Set the MIME content type of the response.

setCookie(mixed $cookie)

Add cookies to the response to send.

setEnvRequest(Message $env_request)

Override the environment's request.

setEtag(string $etag)

Set a custom ETag.

setLastModified(int $last_modified)

Set a custom last modified time stamp.

setThrottleRate(int $chunk_size, float $delay = 1)

Enable throttling.

Details

__construct()

Create a new env response message instance.

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<0, max> count()

Implements Countable.

Return Value

int<0, max>

The custom count as an integer.

The return value is cast to an integer.

TValue current()

Implements iterator.

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

Return Value

TValue

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|null $into_class = null)

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

Parameters

string $header

The header's name.

string|null $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".

TKey|null key()

Implements Iterator.

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

Return Value

TKey|null

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|null $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|null $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.

bool __invoke(string $data, int $ob_flags = 0)

Output buffer handler.

Appends output data to the body.

Parameters

string $data

The data output.

int $ob_flags

Output buffering flags passed from the output buffering control layer.

Return Value

bool success.

int isCachedByEtag(string $header_name = "If-None-Match")

Manually test the header $header_name of the environment's request for a cache hit.

http\Env\Response::send() checks that itself, though.

Parameters

string $header_name

The request header to test.

Return Value

int

a http\Env\Response::CACHE_* constant.

int isCachedByLastModified(string $header_name = "If-Modified-Since")

Manually test the header $header_name of the environment's request for a cache hit.

http\Env\Response::send() checks that itself, though.

Parameters

string $header_name

The request header to test.

Return Value

int

a http\Env\Response::CACHE_* constant.

bool send(resource $stream = null)

Send the response through the SAPI or $stream.

Flushes all output buffers.

Parameters

resource $stream

A writable stream to send the response through.

Return Value

bool success.

Response setCacheControl(string $cache_control)

Make suggestions to the client how it should cache the response.

Parameters

string $cache_control

(A) "Cache-Control" header value(s).

Return Value

Response self.

Exceptions

InvalidArgumentException

Response setContentDisposition(array $disposition_params)

Set the response's content disposition parameters.

Parameters

array $disposition_params

MIME content disposition as http\Params array.

Return Value

Response self.

Exceptions

InvalidArgumentException

Response setContentEncoding(int $content_encoding)

Enable support for "Accept-Encoding" requests with deflate or gzip.

The response will be compressed if the client indicates support and wishes that.

Parameters

int $content_encoding

See http\Env\Response::CONTENTENCODING* constants.

Return Value

Response self.

Exceptions

InvalidArgumentException

Response setContentType(string $content_type)

Set the MIME content type of the response.

Parameters

string $content_type

The response's content type.

Return Value

Response self.

Exceptions

InvalidArgumentException

Response setCookie(mixed $cookie)

Add cookies to the response to send.

Parameters

mixed $cookie

The cookie to send.

Return Value

Response self.

Exceptions

InvalidArgumentException
UnexpectedValueException

Response setEnvRequest(Message $env_request)

Override the environment's request.

Parameters

Message $env_request

The overriding request message.

Return Value

Response self.

Exceptions

InvalidArgumentException

Response setEtag(string $etag)

Set a custom ETag.

NOTE: This will be used for caching and pre-condition checks.

Parameters

string $etag

A ETag.

Return Value

Response self.

Exceptions

InvalidArgumentException

Response setLastModified(int $last_modified)

Set a custom last modified time stamp.

NOTE: This will be used for caching and pre-condition checks.

Parameters

int $last_modified

A unix timestamp.

Return Value

Response self.

Exceptions

InvalidArgumentException

Response setThrottleRate(int $chunk_size, float $delay = 1)

Enable throttling.

Send $chunk_size bytes every $delay seconds.

NOTE: If you need throttling by regular means, check for other options in your stack, because this method blocks the executing process/thread until the response has completely been sent.

Parameters

int $chunk_size

Bytes to send.

float $delay

Seconds to sleep.

Return Value

Response self.