Response
extends Message
in package
implements
ResponseInterface
Represents an HTTP response.
This class extends the Message class and implements the ResponseInterface. It provides methods to handle and manipulate HTTP responses.
Tags
Interfaces, Classes and Traits
- ResponseInterface
- Representation of an outgoing, server-side response.
Table of Contents
- $statusTexts : array<string|int, mixed>
- Status codes and their corresponding text phrases.
- $content : string
- The response content.
- $headers : array<string|int, mixed>
- $protocol : string
- $reasonPhrase : string
- The reason phrase associated with the status code.
- $statusCode : int
- The status code of the response.
- $stream : StreamInterface
- __construct() : mixed
- Create a new Response instance.
- getBody() : StreamInterface
- Get the body of the message.
- getContent() : string
- Get the content of the response.
- getHeader() : array<string|int, string>
- Get the specified header as an array.
- getHeaderLine() : string
- Get the specified header as a string.
- getHeaders() : array<string|int, mixed>
- Get all the headers.
- getProtocolVersion() : string
- Get the protocol version of the message.
- getReasonPhrase() : string
- Get the reason phrase associated with the status code.
- getStatusCode() : int
- Get the status code of the response.
- hasHeader() : bool
- Check if a header exists.
- send() : Response
- Send the HTTP headers and content.
- withAddedHeader() : static
- Add a new header.
- withBody() : static
- Set the message body.
- withContent() : Response
- Set the content of the response.
- withHeader() : static
- Set a header, replacing any existing values.
- withHeaders() : static
- Set multiple headers, replacing any existing values.
- withoutHeader() : static
- Remove a header.
- withProtocolVersion() : static
- Set the protocol version of the message.
- withStatus() : Response
- Set the status code and reason phrase of the response.
- filterHeader() : mixed
- Check the validity of a header.
- getRequestHeaders() : array<string|int, mixed>
- Function to retrieve HTTP headers.
- sendContent() : Response
- Send the response content.
- sendHeaders() : Response
- Send the HTTP headers.
Properties
$statusTexts
Status codes and their corresponding text phrases.
public
static array<string|int, mixed>
$statusTexts
= [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
// RFC2518
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
// RFC4918
208 => 'Already Reported',
// RFC5842
226 => 'IM Used',
// RFC3229
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => 'Reserved',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
// RFC7238
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',
// RFC2324
422 => 'Unprocessable Entity',
// RFC4918
423 => 'Locked',
// RFC4918
424 => 'Failed Dependency',
// RFC4918
425 => 'Reserved for WebDAV advanced collections expired proposal',
// RFC2817
426 => 'Upgrade Required',
// RFC2817
428 => 'Precondition Required',
// RFC6585
429 => 'Too Many Requests',
// RFC6585
431 => 'Request Header Fields Too Large',
// RFC6585
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)',
// RFC2295
507 => 'Insufficient Storage',
// RFC4918
508 => 'Loop Detected',
// RFC5842
510 => 'Not Extended',
// RFC2774
511 => 'Network Authentication Required',
]
$content
The response content.
protected
string
$content
$headers
protected
array<string|int, mixed>
$headers
= []
The headers of the message.
$protocol
protected
string
$protocol
= '1.1'
The protocol version of the message.
$reasonPhrase
The reason phrase associated with the status code.
protected
string
$reasonPhrase
$statusCode
The status code of the response.
protected
int
$statusCode
$stream
protected
StreamInterface
$stream
The body of the message.
Methods
__construct()
Create a new Response instance.
public
__construct([int $code = 200 ][, string|null $reasonPhrase = null ]) : mixed
Parameters
- $code : int = 200
-
The status code.
- $reasonPhrase : string|null = null
-
The reason phrase (optional).
Return values
mixed —getBody()
Get the body of the message.
public
getBody() : StreamInterface
Return values
StreamInterface —The message body.
getContent()
Get the content of the response.
public
getContent() : string
Return values
string —The response content.
getHeader()
Get the specified header as an array.
public
getHeader(string $name) : array<string|int, string>
Parameters
- $name : string
-
The name of the header.
Return values
array<string|int, string> —The values of the header as an array, or an empty array if not found.
getHeaderLine()
Get the specified header as a string.
public
getHeaderLine(string $name) : string
Parameters
- $name : string
-
The name of the header.
Return values
string —The concatenated values of the header, separated by commas, or an empty string if not found.
getHeaders()
Get all the headers.
public
getHeaders() : array<string|int, mixed>
Return values
array<string|int, mixed> —The headers of the message.
getProtocolVersion()
Get the protocol version of the message.
public
getProtocolVersion() : string
Return values
string —The protocol version.
getReasonPhrase()
Get the reason phrase associated with the status code.
public
getReasonPhrase() : string
Tags
Return values
string —The reason phrase.
getStatusCode()
Get the status code of the response.
public
getStatusCode() : int
Return values
int —The status code.
hasHeader()
Check if a header exists.
public
hasHeader(string $name) : bool
Parameters
- $name : string
-
The name of the header.
Return values
bool —True if the header exists, false otherwise.
send()
Send the HTTP headers and content.
public
send() : Response
Return values
Response —The Response object.
withAddedHeader()
Add a new header.
public
withAddedHeader(string $name, string|array<string|int, string> $value) : static
Parameters
- $name : string
-
The name of the header.
- $value : string|array<string|int, string>
-
The value(s) of the header as a string or an array of strings.
Tags
Return values
static —withBody()
Set the message body.
public
withBody(StreamInterface $body) : static
Parameters
- $body : StreamInterface
-
The message body.
Return values
static —withContent()
Set the content of the response.
public
withContent(mixed $content) : Response
Parameters
- $content : mixed
-
The response content.
Tags
Return values
Response —The Response object.
withHeader()
Set a header, replacing any existing values.
public
withHeader(string $name, string|array<string|int, string> $value) : static
Parameters
- $name : string
-
The name of the header.
- $value : string|array<string|int, string>
-
The value(s) of the header as a string or an array of strings.
Tags
Return values
static —withHeaders()
Set multiple headers, replacing any existing values.
public
withHeaders(array<string|int, mixed> $headers) : static
Parameters
- $headers : array<string|int, mixed>
-
An array of headers in the format: array($key => $value, $key => $value...)
Tags
Return values
static —withoutHeader()
Remove a header.
public
withoutHeader(string $name) : static
Parameters
- $name : string
-
The name of the header to remove.
Return values
static —withProtocolVersion()
Set the protocol version of the message.
public
withProtocolVersion(string $version) : static
Parameters
- $version : string
-
The protocol version (e.g., 1.1, 1.0).
Return values
static —withStatus()
Set the status code and reason phrase of the response.
public
withStatus(int $code[, string $reasonPhrase = '' ]) : Response
Parameters
- $code : int
-
The status code.
- $reasonPhrase : string = ''
-
The reason phrase (optional).
Return values
Response —The new Response object.
filterHeader()
Check the validity of a header.
protected
filterHeader(string $name) : mixed
Parameters
- $name : string
-
The name of the header.
Tags
Return values
mixed —getRequestHeaders()
Function to retrieve HTTP headers.
protected
getRequestHeaders() : array<string|int, mixed>
Return values
array<string|int, mixed> —An array of HTTP headers.
sendContent()
Send the response content.
protected
sendContent() : Response
Return values
Response —The Response object.
sendHeaders()
Send the HTTP headers.
protected
sendHeaders() : Response
Return values
Response —The Response object.