ResponseInterface
extends
MessageInterface
in
Representation of an outgoing, server-side response.
Most of these methods are supplied by ResponseTrait.
Per the HTTP specification, this interface includes properties for each of the following:
- Protocol version
- Status code and reason phrase
- Headers
- Message body
Table of Contents
Constants
- HTTP_ACCEPTED = 202
- HTTP_ALREADY_REPORTED = 208
- HTTP_BAD_GATEWAY = 502
- HTTP_BAD_REQUEST = 400
- HTTP_CLIENT_CLOSED_REQUEST = 499
- HTTP_CONFLICT = 409
- HTTP_CONTINUE = 100
- Constants for status codes.
- HTTP_CREATED = 201
- HTTP_EARLY_HINTS = 103
- HTTP_EXPECTATION_FAILED = 417
- HTTP_FAILED_DEPENDENCY = 424
- HTTP_FORBIDDEN = 403
- HTTP_FOUND = 302
- HTTP_GATEWAY_TIMEOUT = 504
- HTTP_GONE = 410
- HTTP_HTTP_VERSION_NOT_SUPPORTED = 505
- HTTP_IM_A_TEAPOT = 418
- HTTP_IM_USED = 226
- HTTP_INSUFFICIENT_STORAGE = 507
- HTTP_INTERNAL_SERVER_ERROR = 500
- HTTP_LENGTH_REQUIRED = 411
- HTTP_LOCKED = 423
- HTTP_LOOP_DETECTED = 508
- HTTP_METHOD_NOT_ALLOWED = 405
- HTTP_MISDIRECTED_REQUEST = 421
- HTTP_MOVED_PERMANENTLY = 301
- HTTP_MULTI_STATUS = 207
- HTTP_MULTIPLE_CHOICES = 300
- HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511
- HTTP_NETWORK_CONNECT_TIMEOUT_ERROR = 599
- HTTP_NO_CONTENT = 204
- HTTP_NONAUTHORITATIVE_INFORMATION = 203
- HTTP_NOT_ACCEPTABLE = 406
- HTTP_NOT_EXTENDED = 510
- HTTP_NOT_FOUND = 404
- HTTP_NOT_IMPLEMENTED = 501
- HTTP_NOT_MODIFIED = 304
- HTTP_OK = 200
- HTTP_PARTIAL_CONTENT = 206
- HTTP_PAYLOAD_TOO_LARGE = 413
- HTTP_PAYMENT_REQUIRED = 402
- HTTP_PERMANENT_REDIRECT = 308
- HTTP_PRECONDITION_FAILED = 412
- HTTP_PRECONDITION_REQUIRED = 428
- HTTP_PROCESSING = 102
- HTTP_PROXY_AUTHENTICATION_REQUIRED = 407
- HTTP_RANGE_NOT_SATISFIABLE = 416
- HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431
- HTTP_REQUEST_TIMEOUT = 408
- HTTP_RESET_CONTENT = 205
- HTTP_SEE_OTHER = 303
- HTTP_SERVICE_UNAVAILABLE = 503
- HTTP_SWITCH_PROXY = 306
- HTTP_SWITCHING_PROTOCOLS = 101
- HTTP_TEMPORARY_REDIRECT = 307
- HTTP_TOO_EARLY = 425
- HTTP_TOO_MANY_REQUESTS = 429
- HTTP_UNAUTHORIZED = 401
- HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451
- HTTP_UNPROCESSABLE_ENTITY = 422
- HTTP_UNSUPPORTED_MEDIA_TYPE = 415
- HTTP_UPGRADE_REQUIRED = 426
- HTTP_URI_TOO_LONG = 414
- HTTP_USE_PROXY = 305
- HTTP_VARIANT_ALSO_NEGOTIATES = 506
Methods
- appendBody() : $this
- Appends data to the body of the current message.
- appendHeader() : $this
- Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
- deleteCookie() : $this
- Sets a cookie to be deleted when the response is sent.
- download() : DownloadResponse|null
- Force a download.
- getBody() : string|null
- Gets the body of the message.
- getCookie() : array<string, Cookie>|Cookie|null
- Returns the cookie
- getCookies() : array<string, Cookie>
- Returns all cookies currently set.
- getCookieStore() : CookieStore
- Returns the `CookieStore` instance.
- getCSP() : ContentSecurityPolicy
- Get Content Security Policy handler.
- getHeaderLine() : string
- Retrieves a comma-separated string of the values for a single header.
- getJSON() : bool|string|null
- Returns the current body, converted to JSON is it isn't already.
- getProtocolVersion() : string
- Retrieves the HTTP protocol version as a string.
- getReason() : string
- Gets the response phrase associated with the status code.
- getReasonPhrase() : string
- Gets the response reason phrase associated with the status code.
- getStatusCode() : int
- Gets the response status code.
- getXML() : bool|string|null
- Retrieves the current body into XML and returns it.
- hasCookie() : bool
- Checks to see if the Response has a specified cookie or not.
- hasHeader() : bool
- Checks if a header exists by the given case-insensitive name.
- header() : Header|array<int, Header>|null
- Returns a single Header object. If multiple headers with the same name exist, then will return an array of header objects.
- headers() : array<string, Header|array<int, Header>>
- Returns an array containing all Headers.
- noCache() : $this
- Sets the appropriate headers to ensure this response is not cached by the browsers.
- populateHeaders() : void
- Populates the $headers array with any headers the server knows about.
- prependHeader() : $this
- Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
- redirect() : $this
- Perform a redirect to a new URL, in two flavors: header or location.
- removeHeader() : $this
- Removes a header from the list of headers we track.
- send() : $this
- Sends the output to the browser.
- sendBody() : $this
- Sends the Body of the message to the browser.
- sendHeaders() : $this
- Sends the headers of this HTTP request to the browser.
- setBody() : $this
- Sets the body of the current message.
- setCache() : $this
- A shortcut method that allows the developer to set all of the cache-control headers in one method call.
- setContentType() : $this
- Sets the Content Type header for this response with the mime type and, optionally, the charset.
- setCookie() : $this
- Set a cookie
- setDate() : $this
- Sets the date header
- setHeader() : $this
- Sets a header and it's value.
- setJSON() : $this
- Converts the $body into JSON and sets the Content Type header.
- setLastModified() : $this
- Sets the Last-Modified date header.
- setLink() : $this
- Set the Link Header
- setProtocolVersion() : $this
- Sets the HTTP protocol version.
- setStatusCode() : $this
- Return an instance with the specified status code and, optionally, reason phrase.
- setXML() : $this
- Converts $body into XML, and sets the correct Content-Type.
Constants
HTTP_ACCEPTED
public
mixed
HTTP_ACCEPTED
= 202
HTTP_ALREADY_REPORTED
public
mixed
HTTP_ALREADY_REPORTED
= 208
HTTP_BAD_GATEWAY
public
mixed
HTTP_BAD_GATEWAY
= 502
HTTP_BAD_REQUEST
public
mixed
HTTP_BAD_REQUEST
= 400
HTTP_CLIENT_CLOSED_REQUEST
public
mixed
HTTP_CLIENT_CLOSED_REQUEST
= 499
HTTP_CONFLICT
public
mixed
HTTP_CONFLICT
= 409
HTTP_CONTINUE
Constants for status codes.
public
mixed
HTTP_CONTINUE
= 100
From https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
HTTP_CREATED
public
mixed
HTTP_CREATED
= 201
HTTP_EARLY_HINTS
public
mixed
HTTP_EARLY_HINTS
= 103
HTTP_EXPECTATION_FAILED
public
mixed
HTTP_EXPECTATION_FAILED
= 417
HTTP_FAILED_DEPENDENCY
public
mixed
HTTP_FAILED_DEPENDENCY
= 424
HTTP_FORBIDDEN
public
mixed
HTTP_FORBIDDEN
= 403
HTTP_FOUND
public
mixed
HTTP_FOUND
= 302
HTTP_GATEWAY_TIMEOUT
public
mixed
HTTP_GATEWAY_TIMEOUT
= 504
HTTP_GONE
public
mixed
HTTP_GONE
= 410
HTTP_HTTP_VERSION_NOT_SUPPORTED
public
mixed
HTTP_HTTP_VERSION_NOT_SUPPORTED
= 505
HTTP_IM_A_TEAPOT
public
mixed
HTTP_IM_A_TEAPOT
= 418
HTTP_IM_USED
public
mixed
HTTP_IM_USED
= 226
HTTP_INSUFFICIENT_STORAGE
public
mixed
HTTP_INSUFFICIENT_STORAGE
= 507
HTTP_INTERNAL_SERVER_ERROR
public
mixed
HTTP_INTERNAL_SERVER_ERROR
= 500
HTTP_LENGTH_REQUIRED
public
mixed
HTTP_LENGTH_REQUIRED
= 411
HTTP_LOCKED
public
mixed
HTTP_LOCKED
= 423
HTTP_LOOP_DETECTED
public
mixed
HTTP_LOOP_DETECTED
= 508
HTTP_METHOD_NOT_ALLOWED
public
mixed
HTTP_METHOD_NOT_ALLOWED
= 405
HTTP_MISDIRECTED_REQUEST
public
mixed
HTTP_MISDIRECTED_REQUEST
= 421
HTTP_MOVED_PERMANENTLY
public
mixed
HTTP_MOVED_PERMANENTLY
= 301
HTTP_MULTI_STATUS
public
mixed
HTTP_MULTI_STATUS
= 207
HTTP_MULTIPLE_CHOICES
public
mixed
HTTP_MULTIPLE_CHOICES
= 300
HTTP_NETWORK_AUTHENTICATION_REQUIRED
public
mixed
HTTP_NETWORK_AUTHENTICATION_REQUIRED
= 511
HTTP_NETWORK_CONNECT_TIMEOUT_ERROR
public
mixed
HTTP_NETWORK_CONNECT_TIMEOUT_ERROR
= 599
HTTP_NO_CONTENT
public
mixed
HTTP_NO_CONTENT
= 204
HTTP_NONAUTHORITATIVE_INFORMATION
public
mixed
HTTP_NONAUTHORITATIVE_INFORMATION
= 203
HTTP_NOT_ACCEPTABLE
public
mixed
HTTP_NOT_ACCEPTABLE
= 406
HTTP_NOT_EXTENDED
public
mixed
HTTP_NOT_EXTENDED
= 510
HTTP_NOT_FOUND
public
mixed
HTTP_NOT_FOUND
= 404
HTTP_NOT_IMPLEMENTED
public
mixed
HTTP_NOT_IMPLEMENTED
= 501
HTTP_NOT_MODIFIED
public
mixed
HTTP_NOT_MODIFIED
= 304
HTTP_OK
public
mixed
HTTP_OK
= 200
HTTP_PARTIAL_CONTENT
public
mixed
HTTP_PARTIAL_CONTENT
= 206
HTTP_PAYLOAD_TOO_LARGE
public
mixed
HTTP_PAYLOAD_TOO_LARGE
= 413
HTTP_PAYMENT_REQUIRED
public
mixed
HTTP_PAYMENT_REQUIRED
= 402
HTTP_PERMANENT_REDIRECT
public
mixed
HTTP_PERMANENT_REDIRECT
= 308
HTTP_PRECONDITION_FAILED
public
mixed
HTTP_PRECONDITION_FAILED
= 412
HTTP_PRECONDITION_REQUIRED
public
mixed
HTTP_PRECONDITION_REQUIRED
= 428
HTTP_PROCESSING
public
mixed
HTTP_PROCESSING
= 102
HTTP_PROXY_AUTHENTICATION_REQUIRED
public
mixed
HTTP_PROXY_AUTHENTICATION_REQUIRED
= 407
HTTP_RANGE_NOT_SATISFIABLE
public
mixed
HTTP_RANGE_NOT_SATISFIABLE
= 416
HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
public
mixed
HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
= 431
HTTP_REQUEST_TIMEOUT
public
mixed
HTTP_REQUEST_TIMEOUT
= 408
HTTP_RESET_CONTENT
public
mixed
HTTP_RESET_CONTENT
= 205
HTTP_SEE_OTHER
public
mixed
HTTP_SEE_OTHER
= 303
HTTP_SERVICE_UNAVAILABLE
public
mixed
HTTP_SERVICE_UNAVAILABLE
= 503
HTTP_SWITCH_PROXY
public
mixed
HTTP_SWITCH_PROXY
= 306
HTTP_SWITCHING_PROTOCOLS
public
mixed
HTTP_SWITCHING_PROTOCOLS
= 101
HTTP_TEMPORARY_REDIRECT
public
mixed
HTTP_TEMPORARY_REDIRECT
= 307
HTTP_TOO_EARLY
public
mixed
HTTP_TOO_EARLY
= 425
HTTP_TOO_MANY_REQUESTS
public
mixed
HTTP_TOO_MANY_REQUESTS
= 429
HTTP_UNAUTHORIZED
public
mixed
HTTP_UNAUTHORIZED
= 401
HTTP_UNAVAILABLE_FOR_LEGAL_REASONS
public
mixed
HTTP_UNAVAILABLE_FOR_LEGAL_REASONS
= 451
HTTP_UNPROCESSABLE_ENTITY
public
mixed
HTTP_UNPROCESSABLE_ENTITY
= 422
HTTP_UNSUPPORTED_MEDIA_TYPE
public
mixed
HTTP_UNSUPPORTED_MEDIA_TYPE
= 415
HTTP_UPGRADE_REQUIRED
public
mixed
HTTP_UPGRADE_REQUIRED
= 426
HTTP_URI_TOO_LONG
public
mixed
HTTP_URI_TOO_LONG
= 414
HTTP_USE_PROXY
public
mixed
HTTP_USE_PROXY
= 305
HTTP_VARIANT_ALSO_NEGOTIATES
public
mixed
HTTP_VARIANT_ALSO_NEGOTIATES
= 506
Methods
appendBody()
Appends data to the body of the current message.
public
appendBody(string $data) : $this
Parameters
- $data : string
Return values
$thisappendHeader()
Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
public
appendHeader(string $name, string|null $value) : $this
Parameters
- $name : string
- $value : string|null
Return values
$thisdeleteCookie()
Sets a cookie to be deleted when the response is sent.
public
deleteCookie([string $name = '' ][, string $domain = '' ][, string $path = '/' ][, string $prefix = '' ]) : $this
Parameters
- $name : string = ''
- $domain : string = ''
- $path : string = '/'
- $prefix : string = ''
Return values
$thisdownload()
Force a download.
public
download([string $filename = '' ][, string|null $data = '' ][, bool $setMime = false ]) : DownloadResponse|null
Generates the headers that force a download to happen. And sends the file to the browser.
Parameters
- $filename : string = ''
-
The path to the file to send
- $data : string|null = ''
-
The data to be downloaded
- $setMime : bool = false
-
Whether to try and send the actual MIME type
Return values
DownloadResponse|nullgetBody()
Gets the body of the message.
public
getBody() : string|null
Tags
Return values
string|nullgetCookie()
Returns the cookie
public
getCookie([string|null $name = null ][, string $prefix = '' ]) : array<string, Cookie>|Cookie|null
Parameters
- $name : string|null = null
- $prefix : string = ''
Return values
array<string, Cookie>|Cookie|nullgetCookies()
Returns all cookies currently set.
public
getCookies() : array<string, Cookie>
Return values
array<string, Cookie>getCookieStore()
Returns the `CookieStore` instance.
public
getCookieStore() : CookieStore
Return values
CookieStoregetCSP()
Get Content Security Policy handler.
public
getCSP() : ContentSecurityPolicy
Return values
ContentSecurityPolicygetHeaderLine()
Retrieves a comma-separated string of the values for a single header.
public
getHeaderLine(string $name) : string
This method returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma.
NOTE: Not all header values may be appropriately represented using comma concatenation. For such headers, use getHeader() instead and supply your own delimiter when concatenating.
Parameters
- $name : string
Return values
stringgetJSON()
Returns the current body, converted to JSON is it isn't already.
public
getJSON() : bool|string|null
Tags
Return values
bool|string|nullgetProtocolVersion()
Retrieves the HTTP protocol version as a string.
public
getProtocolVersion() : string
The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
Return values
string —HTTP protocol version.
getReason()
Gets the response phrase associated with the status code.
public
getReason() : string
Use getReasonPhrase()
Tags
Return values
stringgetReasonPhrase()
Gets the response reason phrase associated with the status code.
public
getReasonPhrase() : string
Because a reason phrase is not a required element in a response status line, the reason phrase value MAY be null. Implementations MAY choose to return the default RFC 7231 recommended reason phrase (or those listed in the IANA HTTP Status Code Registry) for the response's status code.
Tags
Return values
string —Reason phrase; must return an empty string if none present.
getStatusCode()
Gets the response status code.
public
getStatusCode() : int
The status code is a 3-digit integer result code of the server's attempt to understand and satisfy the request.
Return values
int —Status code.
getXML()
Retrieves the current body into XML and returns it.
public
getXML() : bool|string|null
Tags
Return values
bool|string|nullhasCookie()
Checks to see if the Response has a specified cookie or not.
public
hasCookie(string $name[, string|null $value = null ][, string $prefix = '' ]) : bool
Parameters
- $name : string
- $value : string|null = null
- $prefix : string = ''
Return values
boolhasHeader()
Checks if a header exists by the given case-insensitive name.
public
hasHeader(string $name) : bool
Parameters
- $name : string
-
Case-insensitive header field name.
Return values
bool —Returns true if any header names match the given header name using a case-insensitive string comparison. Returns false if no matching header name is found in the message.
header()
Returns a single Header object. If multiple headers with the same name exist, then will return an array of header objects.
public
header(string $name) : Header|array<int, Header>|null
Parameters
- $name : string
Return values
Header|array<int, Header>|nullheaders()
Returns an array containing all Headers.
public
headers() : array<string, Header|array<int, Header>>
Return values
array<string, Header|array<int, Header>> —An array of the Header objects
noCache()
Sets the appropriate headers to ensure this response is not cached by the browsers.
public
noCache() : $this
Return values
$thispopulateHeaders()
Populates the $headers array with any headers the server knows about.
public
populateHeaders() : void
prependHeader()
Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
public
prependHeader(string $name, string $value) : $this
Parameters
- $name : string
- $value : string
Return values
$thisredirect()
Perform a redirect to a new URL, in two flavors: header or location.
public
redirect(string $uri[, string $method = 'auto' ][, int $code = null ]) : $this
Parameters
- $uri : string
-
The URI to redirect to
- $method : string = 'auto'
- $code : int = null
-
The type of redirection, defaults to 302
Tags
Return values
$thisremoveHeader()
Removes a header from the list of headers we track.
public
removeHeader(string $name) : $this
Parameters
- $name : string
Return values
$thissend()
Sends the output to the browser.
public
send() : $this
Return values
$thissendBody()
Sends the Body of the message to the browser.
public
sendBody() : $this
Return values
$thissendHeaders()
Sends the headers of this HTTP request to the browser.
public
sendHeaders() : $this
Return values
$thissetBody()
Sets the body of the current message.
public
setBody(string $data) : $this
Parameters
- $data : string
Return values
$thissetCache()
A shortcut method that allows the developer to set all of the cache-control headers in one method call.
public
setCache([array<string|int, mixed> $options = [] ]) : $this
The options array is used to provide the cache-control directives for the header. It might look something like:
$options = [
'max-age' => 300,
's-maxage' => 900
'etag' => 'abcde',
];
Typical options are:
- etag
- last-modified
- max-age
- s-maxage
- private
- public
- must-revalidate
- proxy-revalidate
- no-transform
Parameters
- $options : array<string|int, mixed> = []
Return values
$thissetContentType()
Sets the Content Type header for this response with the mime type and, optionally, the charset.
public
setContentType(string $mime[, string $charset = 'UTF-8' ]) : $this
Parameters
- $mime : string
- $charset : string = 'UTF-8'
Return values
$thissetCookie()
Set a cookie
public
setCookie(array<string|int, mixed>|Cookie|string $name[, string $value = '' ][, int $expire = 0 ][, string $domain = '' ][, string $path = '/' ][, string $prefix = '' ][, bool $secure = false ][, bool $httponly = false ][, string|null $samesite = null ]) : $this
Accepts an arbitrary number of binds (up to 7) or an associative array in the first parameter containing all the values.
Parameters
- $name : array<string|int, mixed>|Cookie|string
-
Cookie name / array containing binds / Cookie object
- $value : string = ''
-
Cookie value
- $expire : int = 0
-
Cookie expiration time in seconds
- $domain : string = ''
-
Cookie domain (e.g.: '.yourdomain.com')
- $path : string = '/'
-
Cookie path (default: '/')
- $prefix : string = ''
-
Cookie name prefix
- $secure : bool = false
-
Whether to only transfer cookies via SSL
- $httponly : bool = false
-
Whether only make the cookie accessible via HTTP (no javascript)
- $samesite : string|null = null
Return values
$thissetDate()
Sets the date header
public
setDate(DateTime $date) : $this
Parameters
- $date : DateTime
Return values
$thissetHeader()
Sets a header and it's value.
public
setHeader(string $name, array<string|int, mixed>|string|null $value) : $this
Parameters
- $name : string
- $value : array<string|int, mixed>|string|null
Return values
$thissetJSON()
Converts the $body into JSON and sets the Content Type header.
public
setJSON(array<string|int, mixed>|object|string $body[, bool $unencoded = false ]) : $this
Parameters
- $body : array<string|int, mixed>|object|string
- $unencoded : bool = false
Return values
$thissetLastModified()
Sets the Last-Modified date header.
public
setLastModified(DateTime|string $date) : $this
$date can be either a string representation of the date or, preferably, an instance of DateTime.
Parameters
- $date : DateTime|string
Return values
$thissetLink()
Set the Link Header
public
setLink(PagerInterface $pager) : $this
Parameters
- $pager : PagerInterface
Tags
Return values
$thissetProtocolVersion()
Sets the HTTP protocol version.
public
setProtocolVersion(string $version) : $this
Parameters
- $version : string
Tags
Return values
$thissetStatusCode()
Return an instance with the specified status code and, optionally, reason phrase.
public
setStatusCode(int $code[, string $reason = '' ]) : $this
If no reason phrase is specified, will default recommended reason phrase for the response's status code.
Parameters
- $code : int
-
The 3-digit integer result code to set.
- $reason : string = ''
-
The reason phrase to use with the provided status code; if none is provided, will default to the IANA name.
Tags
Return values
$thissetXML()
Converts $body into XML, and sets the correct Content-Type.
public
setXML(array<string|int, mixed>|string $body) : $this
Parameters
- $body : array<string|int, mixed>|string