Response
extends AbstractResponse
in package
HTTP server response class
Tags
Table of Contents
Properties
- $body : Body|null
- Body
- $code : int|null
- Response code
- $headers : array<string|int, mixed>
- Headers
- $message : string|null
- Response message
- $responseCodes : array<string|int, mixed>
- Response codes & messages
- $version : string
- HTTP version for response, i.e. 1.0, 1.1, 2.0, etc.
Methods
- __construct() : mixed
- Constructor
- __get() : mixed
- Magic method to get either the headers or body
- __toString() : string
- Return entire response as a string
- addHeader() : AbstractRequestResponse
- Add a header
- addHeaders() : AbstractRequestResponse
- Add all headers
- create() : static
- Factory method to create a Response object
- decodeBodyContent() : Body
- Decode the body
- forward() : void
- Forward a client response as the server response
- forwardAndExit() : void
- Forward a client response as the server response and exit
- getBody() : Body
- Get the body
- getBodyContent() : mixed
- Get body content
- getBodyContentLength() : int
- Get body content length
- getCode() : int
- Get the response code
- getHeader() : mixed
- Get a header
- getHeaderAsString() : mixed
- Get a header
- getHeaders() : array<string|int, mixed>
- Get all headers
- getHeadersAsArray() : array<string|int, mixed>
- Get all header values as associative array
- getHeadersAsString() : string
- Get the response headers as a string
- getHeaderValue() : mixed
- Get header value
- getHeaderValueAsString() : string|null
- Get header value as string
- getMessage() : string
- Get the response HTTP message
- getMessageFromCode() : string
- Get response message from code
- getVersion() : string
- Get the response HTTP version
- hasBody() : bool
- Has a body
- hasBodyContent() : bool
- Has body content
- hasCode() : bool
- Has the response code
- hasHeader() : bool
- Has a header
- hasHeaders() : bool
- Determine if there are headers
- hasMessage() : bool
- Has the response message
- hasVersion() : bool
- Has the response HTTP version
- isAccepted() : bool
- Determine if the response is 202 accepted
- isBadGateway() : bool
- Determine if the response is a 502 Bad Gateway
- isBadRequest() : bool
- Determine if the response is a 400 Bad Request
- isClientError() : bool
- Determine if the response is a client error
- isConflict() : bool
- Determine if the response is a 409 Conflict
- isContinue() : bool
- Determine if the response is a 100 continue
- isCreated() : bool
- Determine if the response is 201 created
- isError() : bool
- Determine if the response is an error
- isForbidden() : bool
- Determine if the response is a 403 Forbidden
- isFound() : bool
- Determine if the response is a 302 Found
- isInternalServerError() : bool
- Determine if the response is a 500 Internal Server Error
- isLengthRequired() : bool
- Determine if the response is a 411 Length Required
- isMethodNotAllowed() : bool
- Determine if the response is a 405 Method Not Allowed
- isMovedPermanently() : bool
- Determine if the response is a 301 Moved Permanently
- isNoContent() : bool
- Determine if the response is 204 No Content
- isNotAcceptable() : bool
- Determine if the response is a 406 Not Acceptable
- isNotFound() : bool
- Determine if the response is a 404 Not Found
- isOk() : bool
- Determine if the response is 200 OK
- isRedirect() : bool
- Determine if the response is a redirect
- isRequestTimeout() : bool
- Determine if the response is a 408 Request Timeout
- isServerError() : bool
- Determine if the response is a server error
- isServiceUnavailable() : bool
- Determine if the response is a 503 Service Unavailable
- isSuccess() : bool
- Determine if the response is a success
- isTooManyRequests() : bool
- Determine if the response is a 429 Too Many Requests
- isUnauthorized() : bool
- Determine if the response is a 401 Unauthorized
- isUnprocessableEntity() : bool
- Determine if the response is a 422 Unprocessable Entity
- isUnsupportedMediaType() : bool
- Determine if the response is a 415 Unsupported Media Type
- prepareBody() : string
- Prepare response body
- redirect() : void
- Send redirect
- redirectAndExit() : void
- Send redirect and exit
- removeBody() : AbstractRequestResponse
- Remove the body
- removeHeader() : AbstractRequestResponse
- Remove a header
- removeHeaders() : AbstractRequestResponse
- Remove all headers
- render() : string
- Render entire response as a string
- send() : void
- Send full response
- sendAndExit() : void
- Send full response and exit
- sendHeaders() : void
- Send response headers
- setBody() : AbstractRequestResponse
- Set the body
- setCode() : AbstractResponse
- Set the response code
- setHeaders() : AbstractRequestResponse
- Set all headers (clear out any existing headers)
- setMessage() : AbstractResponse
- Set the response message
- setVersion() : AbstractResponse
- Set the response version
Properties
$body
Body
protected
Body|null
$body
= null
$code
Response code
protected
int|null
$code
= null
$headers
Headers
protected
array<string|int, mixed>
$headers
= []
$message
Response message
protected
string|null
$message
= null
$responseCodes
Response codes & messages
protected
static array<string|int, mixed>
$responseCodes
= [
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
// Success 2xx
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',
// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => 'Switch Proxy',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
// Client Error 4xx
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 => 'Payload Too Large',
414 => 'URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Range Not Satisfiable',
417 => 'Expectation Failed',
421 => 'Misdirected Request',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
451 => 'Unavailable For Legal Reasons',
// Server Error 5xx
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',
508 => 'Loop Detected',
509 => 'Bandwidth Limit Exceeded',
510 => 'Not Extended',
511 => 'Network Authentication Required',
]
$version
HTTP version for response, i.e. 1.0, 1.1, 2.0, etc.
protected
string
$version
= '1.1'
Methods
__construct()
Constructor
public
__construct([array<string|int, mixed> $config = [] ]) : mixed
Instantiate the response object
Parameters
- $config : array<string|int, mixed> = []
__get()
Magic method to get either the headers or body
public
__get(string $name) : mixed
Parameters
- $name : string
__toString()
Return entire response as a string
public
__toString() : string
Return values
stringaddHeader()
Add a header
public
addHeader(Header|string|int $header[, string|null $value = null ]) : AbstractRequestResponse
Parameters
- $header : Header|string|int
- $value : string|null = null
Return values
AbstractRequestResponseaddHeaders()
Add all headers
public
addHeaders(array<string|int, mixed> $headers) : AbstractRequestResponse
Parameters
- $headers : array<string|int, mixed>
Return values
AbstractRequestResponsecreate()
Factory method to create a Response object
public
static create([array<string|int, mixed> $config = [] ]) : static
Parameters
- $config : array<string|int, mixed> = []
Return values
staticdecodeBodyContent()
Decode the body
public
decodeBodyContent([string|null $body = null ]) : Body
Parameters
- $body : string|null = null
Return values
Bodyforward()
Forward a client response as the server response
public
static forward(Response $clientResponse) : void
Parameters
- $clientResponse : Response
Tags
forwardAndExit()
Forward a client response as the server response and exit
public
static forwardAndExit(Response $clientResponse) : void
Parameters
- $clientResponse : Response
Tags
getBody()
Get the body
public
getBody() : Body
Return values
BodygetBodyContent()
Get body content
public
getBodyContent() : mixed
getBodyContentLength()
Get body content length
public
getBodyContentLength([bool $mb = false ]) : int
Parameters
- $mb : bool = false
Return values
intgetCode()
Get the response code
public
getCode() : int
Return values
intgetHeader()
Get a header
public
getHeader(string $name) : mixed
Parameters
- $name : string
getHeaderAsString()
Get a header
public
getHeaderAsString(string $name) : mixed
Parameters
- $name : string
getHeaders()
Get all headers
public
getHeaders() : array<string|int, mixed>
Return values
array<string|int, mixed>getHeadersAsArray()
Get all header values as associative array
public
getHeadersAsArray([bool $asStrings = true ]) : array<string|int, mixed>
Parameters
- $asStrings : bool = true
Return values
array<string|int, mixed>getHeadersAsString()
Get the response headers as a string
public
getHeadersAsString([mixed $status = null ][, string $eol = "
" ]) : string
Parameters
- $status : mixed = null
- $eol : string = " "
Return values
stringgetHeaderValue()
Get header value
public
getHeaderValue(string $name[, int $i = 0 ]) : mixed
Parameters
- $name : string
- $i : int = 0
getHeaderValueAsString()
Get header value as string
public
getHeaderValueAsString(string $name[, int $i = 0 ]) : string|null
Parameters
- $name : string
- $i : int = 0
Return values
string|nullgetMessage()
Get the response HTTP message
public
getMessage() : string
Return values
stringgetMessageFromCode()
Get response message from code
public
static getMessageFromCode(int $code) : string
Parameters
- $code : int
Tags
Return values
stringgetVersion()
Get the response HTTP version
public
getVersion() : string
Return values
stringhasBody()
Has a body
public
hasBody() : bool
Return values
boolhasBodyContent()
Has body content
public
hasBodyContent() : bool
Return values
boolhasCode()
Has the response code
public
hasCode() : bool
Return values
boolhasHeader()
Has a header
public
hasHeader(string $name) : bool
Parameters
- $name : string
Return values
boolhasHeaders()
Determine if there are headers
public
hasHeaders() : bool
Return values
boolhasMessage()
Has the response message
public
hasMessage() : bool
Return values
boolhasVersion()
Has the response HTTP version
public
hasVersion() : bool
Return values
boolisAccepted()
Determine if the response is 202 accepted
public
isAccepted() : bool
Return values
boolisBadGateway()
Determine if the response is a 502 Bad Gateway
public
isBadGateway() : bool
Return values
boolisBadRequest()
Determine if the response is a 400 Bad Request
public
isBadRequest() : bool
Return values
boolisClientError()
Determine if the response is a client error
public
isClientError() : bool
Return values
boolisConflict()
Determine if the response is a 409 Conflict
public
isConflict() : bool
Return values
boolisContinue()
Determine if the response is a 100 continue
public
isContinue() : bool
Return values
boolisCreated()
Determine if the response is 201 created
public
isCreated() : bool
Return values
boolisError()
Determine if the response is an error
public
isError() : bool
Return values
boolisForbidden()
Determine if the response is a 403 Forbidden
public
isForbidden() : bool
Return values
boolisFound()
Determine if the response is a 302 Found
public
isFound() : bool
Return values
boolisInternalServerError()
Determine if the response is a 500 Internal Server Error
public
isInternalServerError() : bool
Return values
boolisLengthRequired()
Determine if the response is a 411 Length Required
public
isLengthRequired() : bool
Return values
boolisMethodNotAllowed()
Determine if the response is a 405 Method Not Allowed
public
isMethodNotAllowed() : bool
Return values
boolisMovedPermanently()
Determine if the response is a 301 Moved Permanently
public
isMovedPermanently() : bool
Return values
boolisNoContent()
Determine if the response is 204 No Content
public
isNoContent() : bool
Return values
boolisNotAcceptable()
Determine if the response is a 406 Not Acceptable
public
isNotAcceptable() : bool
Return values
boolisNotFound()
Determine if the response is a 404 Not Found
public
isNotFound() : bool
Return values
boolisOk()
Determine if the response is 200 OK
public
isOk() : bool
Return values
boolisRedirect()
Determine if the response is a redirect
public
isRedirect() : bool
Return values
boolisRequestTimeout()
Determine if the response is a 408 Request Timeout
public
isRequestTimeout() : bool
Return values
boolisServerError()
Determine if the response is a server error
public
isServerError() : bool
Return values
boolisServiceUnavailable()
Determine if the response is a 503 Service Unavailable
public
isServiceUnavailable() : bool
Return values
boolisSuccess()
Determine if the response is a success
public
isSuccess() : bool
Return values
boolisTooManyRequests()
Determine if the response is a 429 Too Many Requests
public
isTooManyRequests() : bool
Return values
boolisUnauthorized()
Determine if the response is a 401 Unauthorized
public
isUnauthorized() : bool
Return values
boolisUnprocessableEntity()
Determine if the response is a 422 Unprocessable Entity
public
isUnprocessableEntity() : bool
Return values
boolisUnsupportedMediaType()
Determine if the response is a 415 Unsupported Media Type
public
isUnsupportedMediaType() : bool
Return values
boolprepareBody()
Prepare response body
public
prepareBody([bool $length = false ][, bool $mb = false ]) : string
Parameters
- $length : bool = false
- $mb : bool = false
Return values
stringredirect()
Send redirect
public
static redirect(string $url[, int $code = 302 ][, string $version = '1.1' ]) : void
Parameters
- $url : string
- $code : int = 302
- $version : string = '1.1'
Tags
redirectAndExit()
Send redirect and exit
public
static redirectAndExit(string $url[, int $code = 302 ][, string $version = '1.1' ]) : void
Parameters
- $url : string
- $code : int = 302
- $version : string = '1.1'
Tags
removeBody()
Remove the body
public
removeBody() : AbstractRequestResponse
Return values
AbstractRequestResponseremoveHeader()
Remove a header
public
removeHeader(string $name) : AbstractRequestResponse
Parameters
- $name : string
Return values
AbstractRequestResponseremoveHeaders()
Remove all headers
public
removeHeaders() : AbstractRequestResponse
Return values
AbstractRequestResponserender()
Render entire response as a string
public
render() : string
Return values
stringsend()
Send full response
public
send([int|null $code = null ][, array<string|int, mixed>|null $headers = null ][, bool $length = false ]) : void
Parameters
- $code : int|null = null
- $headers : array<string|int, mixed>|null = null
- $length : bool = false
Tags
sendAndExit()
Send full response and exit
public
sendAndExit([int|null $code = null ][, array<string|int, mixed>|null $headers = null ][, bool $length = false ]) : void
Parameters
- $code : int|null = null
- $headers : array<string|int, mixed>|null = null
- $length : bool = false
Tags
sendHeaders()
Send response headers
public
sendHeaders() : void
Tags
setBody()
Set the body
public
setBody(string|Body $body) : AbstractRequestResponse
Parameters
- $body : string|Body
Return values
AbstractRequestResponsesetCode()
Set the response code
public
setCode([int $code = 200 ]) : AbstractResponse
Parameters
- $code : int = 200
Tags
Return values
AbstractResponsesetHeaders()
Set all headers (clear out any existing headers)
public
setHeaders(array<string|int, mixed> $headers) : AbstractRequestResponse
Parameters
- $headers : array<string|int, mixed>
Return values
AbstractRequestResponsesetMessage()
Set the response message
public
setMessage([string|null $message = null ]) : AbstractResponse
Parameters
- $message : string|null = null
Return values
AbstractResponsesetVersion()
Set the response version
public
setVersion(float|string $version) : AbstractResponse
Parameters
- $version : float|string