title | permalink |
---|---|
Response |
/classes/response |
- Table of Contents {:toc}
The Response
class represents an HTTP response. The Response
promise is returned by request handlers and middleware.
public function __construct(
int $code = Status::OK,
string[] | string[][] $headers = [],
Amp\ByteStream\InputStream | string | null $stringOrStream = null
)
|int
|$code
|HTTP response status code|
|string[]
string[][]
|$headers
|An array of strings or an array of string arrays.|
|Amp\ByteStream\InputStream
string
null
|$stringOrStream
|Response body|
Invokes dispose handlers (i.e. functions that registered via onDispose
method).
{:.note}
Uncaught exceptions from the dispose handlers will be forwarded to the event loop error handler.
Returns the stream for the message body.
Sets the stream for the message body.
{:.note}
Using a string will automatically set the
Content-Length
header to the length of the given string. Setting anInputStream
will remove theContent-Length
header. If you know the exact content length of your stream, you can add acontent-length
header after callingsetBody()
.
Returns the headers as a string-indexed array of arrays of strings or an empty array if no headers have been set.
Checks if given header exists.
Returns the array of values for the given header or an empty array if the header does not exist.
Returns the value of the given header.
If multiple headers are present for the named header, only the first header value will be returned.
Use getHeaderArray()
to return an array of all values for the particular header.
Returns null
if the header does not exist.
Sets the headers from the given array.
Sets the header to the given value(s). All previous header lines with the given name will be replaced.
Adds an additional header line with the given name.
Removes the given header if it exists. If multiple header lines with the same name exist, all of them are removed.
Returns the response status code.
Returns the reason phrase describing the status code.
Sets the numeric HTTP status code (between 100 and 599) and reason phrase. Use null for the reason phrase to use the default phrase associated with the status code.
Returns all cookies in an associative map.
Gets a cookie value by name or null
if no cookie with that name is present.
Adds a cookie to the response.
Removes a cookie from the response.
Returns list of push resources in an associative map:
[
string $url => [ Psr\Http\Message\UriInterface $uri, string[][] $headers ],
]
Indicate resources which a client likely needs to fetch. (e.g. Link: preload
or HTTP/2 Server Push).
Returns true
if a detach callback has been set, false
if none.
Sets a callback to be invoked once the response has been written to the client and changes the status of the response to 101 Switching Protocols
.
The callback may be removed by changing the status to something else.
Returns the upgrade function if present.
Registers a function that is invoked when the Response is discarded. A response is discarded either once it has been written to the client or if it gets replaced in a middleware chain.