Response
extends Message
in package
implements
ResponseInterface
This class represents a single HTTP response.
Tags
Interfaces, Classes and Traits
- ResponseInterface
- This interface represents a HTTP response.
Table of Contents
- $statusCodes : array<string|int, mixed>
- This is the list of currently registered HTTP status codes.
- $body : resource|string|callable
- Request body.
- $headers : array<string|int, mixed>
- Contains the list of HTTP headers.
- $httpVersion : string
- HTTP message version (1.0, 1.1 or 2.0).
- $status : int
- HTTP status code.
- $statusText : string
- HTTP status text.
- __construct() : mixed
- Creates the response object.
- __toString() : string
- Serializes the response object as a string.
- addHeader() : mixed
- Adds a HTTP header.
- addHeaders() : mixed
- Adds a new set of HTTP headers.
- getBody() : resource|string|callable
- Returns the message body, as it's internal representation.
- getBodyAsStream() : resource
- Returns the body as a readable stream resource.
- getBodyAsString() : string
- Returns the body as a string.
- getHeader() : string|null
- Returns a specific HTTP header, based on it's name.
- getHeaderAsArray() : array<string|int, string>
- Returns a HTTP header as an array.
- getHeaders() : array<string|int, mixed>
- Returns all the HTTP headers as an array.
- getHttpVersion() : string
- Returns the HTTP version.
- getStatus() : int
- Returns the current HTTP status code.
- getStatusText() : string
- Returns the human-readable status string.
- hasHeader() : bool
- Will return true or false, depending on if a HTTP header exists.
- removeHeader() : bool
- Removes a HTTP header.
- setBody() : mixed
- Replaces the body resource with a new stream, string or a callback writing the body to php://output.
- setHeader() : mixed
- Updates a HTTP header.
- setHeaders() : mixed
- Sets a new set of HTTP headers.
- setHttpVersion() : mixed
- Sets the HTTP version.
- setStatus() : mixed
- Sets the HTTP status code.
Properties
$statusCodes
This is the list of currently registered HTTP status codes.
public
static array<string|int, mixed>
$statusCodes
= [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authorative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
// RFC 4918
208 => 'Already Reported',
// RFC 5842
226 => 'IM Used',
// RFC 3229
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',
418 => 'I'm a teapot',
// RFC 2324
421 => 'Misdirected Request',
// RFC7540 (HTTP/2)
422 => 'Unprocessable Entity',
// RFC 4918
423 => 'Locked',
// RFC 4918
424 => 'Failed Dependency',
// RFC 4918
426 => 'Upgrade Required',
428 => 'Precondition Required',
// RFC 6585
429 => 'Too Many Requests',
// RFC 6585
431 => 'Request Header Fields Too Large',
// RFC 6585
451 => 'Unavailable For Legal Reasons',
// draft-tbray-http-legally-restricted-status
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',
507 => 'Insufficient Storage',
// RFC 4918
508 => 'Loop Detected',
// RFC 5842
509 => 'Bandwidth Limit Exceeded',
// non-standard
510 => 'Not extended',
511 => 'Network Authentication Required',
]
$body
Request body.
protected
resource|string|callable
$body
This should be a stream resource, string or a callback writing the body to php://output
$headers
Contains the list of HTTP headers.
protected
array<string|int, mixed>
$headers
= []
$httpVersion
HTTP message version (1.0, 1.1 or 2.0).
protected
string
$httpVersion
= '1.1'
$status
HTTP status code.
protected
int
$status
$statusText
HTTP status text.
protected
string
$statusText
Methods
__construct()
Creates the response object.
public
__construct([string|int $status = 500 ][, array<string|int, mixed> $headers = null ][, resource $body = null ]) : mixed
Parameters
- $status : string|int = 500
- $headers : array<string|int, mixed> = null
- $body : resource = null
Return values
mixed —__toString()
Serializes the response object as a string.
public
__toString() : string
This is useful for debugging purposes.
Return values
string —addHeader()
Adds a HTTP header.
public
addHeader(string $name, string|array<string|int, string> $value) : mixed
This method will not overwrite any existing HTTP header, but instead add another value. Individual values can be retrieved with getHeadersAsArray.
Parameters
- $name : string
- $value : string|array<string|int, string>
Return values
mixed —addHeaders()
Adds a new set of HTTP headers.
public
addHeaders(array<string|int, mixed> $headers) : mixed
Any existing headers will not be overwritten.
Parameters
- $headers : array<string|int, mixed>
Return values
mixed —getBody()
Returns the message body, as it's internal representation.
public
getBody() : resource|string|callable
This could be either a string, a stream or a callback writing the body to php://output.
Return values
resource|string|callable —getBodyAsStream()
Returns the body as a readable stream resource.
public
getBodyAsStream() : resource
Note that the stream may not be rewindable, and therefore may only be read once.
Return values
resource —getBodyAsString()
Returns the body as a string.
public
getBodyAsString() : string
Note that because the underlying data may be based on a stream, this method could only work correctly the first time.
Return values
string —getHeader()
Returns a specific HTTP header, based on it's name.
public
getHeader(string $name) : string|null
The name must be treated as case-insensitive. If the header does not exist, this method must return null.
If a header appeared more than once in a HTTP request, this method will concatenate all the values with a comma.
Note that this not make sense for all headers. Some, such as
Set-Cookie cannot be logically combined with a comma. In those cases
you should use getHeaderAsArray().
Parameters
- $name : string
Return values
string|null —getHeaderAsArray()
Returns a HTTP header as an array.
public
getHeaderAsArray(string $name) : array<string|int, string>
For every time the HTTP header appeared in the request or response, an item will appear in the array.
If the header did not exists, this method will return an empty array.
Parameters
- $name : string
Return values
array<string|int, string> —getHeaders()
Returns all the HTTP headers as an array.
public
getHeaders() : array<string|int, mixed>
Every header is returned as an array, with one or more values.
Return values
array<string|int, mixed> —getHttpVersion()
Returns the HTTP version.
public
getHttpVersion() : string
Return values
string —getStatus()
Returns the current HTTP status code.
public
getStatus() : int
Return values
int —getStatusText()
Returns the human-readable status string.
public
getStatusText() : string
In the case of a 200, this may for example be 'OK'.
Return values
string —hasHeader()
Will return true or false, depending on if a HTTP header exists.
public
hasHeader(string $name) : bool
Parameters
- $name : string
Return values
bool —removeHeader()
Removes a HTTP header.
public
removeHeader(string $name) : bool
The specified header name must be treated as case-insensitive. This method should return true if the header was successfully deleted, and false if the header did not exist.
Parameters
- $name : string
Return values
bool —setBody()
Replaces the body resource with a new stream, string or a callback writing the body to php://output.
public
setBody(resource|string|callable $body) : mixed
Parameters
- $body : resource|string|callable
Return values
mixed —setHeader()
Updates a HTTP header.
public
setHeader(string $name, string|array<string|int, string> $value) : mixed
The case-sensitivity of the name value must be retained as-is.
If the header already existed, it will be overwritten.
Parameters
- $name : string
- $value : string|array<string|int, string>
Return values
mixed —setHeaders()
Sets a new set of HTTP headers.
public
setHeaders(array<string|int, mixed> $headers) : mixed
The headers array should contain headernames for keys, and their value should be specified as either a string or an array.
Any header that already existed will be overwritten.
Parameters
- $headers : array<string|int, mixed>
Return values
mixed —setHttpVersion()
Sets the HTTP version.
public
setHttpVersion(string $version) : mixed
Should be 1.0, 1.1 or 2.0.
Parameters
- $version : string
Return values
mixed —setStatus()
Sets the HTTP status code.
public
setStatus(string|int $status) : mixed
This can be either the full HTTP status code with human readable string, for example: "403 I can't let you do that, Dave".
Or just the code, in which case the appropriate default message will be added.
Parameters
- $status : string|int