Class
Request
A PSR-7 outgoing, client-side request.
Introduction#
The request-target and the URI are independent, as PSR-7 has them: the request-target is the
source of truth, and Request::getUri derives a URI from it and the Host header, unless one
was set with Request::withUri.
Class synopsis#
class Request implements RequestInterface, MessageInterface {
/* Methods */
public Request::__construct(
string $method,
string $requestTarget,
?StreamInterface $body = null,
array $headers = [],
string $protocolVersion = '1.1'
)
public Request::__clone()
public static Request::create(
string $method,
Stringable|UriInterface|string $uri
): RequestInterface
public Request::getRequestTarget(): string
public Request::withRequestTarget($requestTarget): RequestInterface
public Request::getMethod(): string
public Request::withMethod($method): RequestInterface
public Request::getUri(): UriInterface
public Request::withUri($uri, $preserveHost = false): RequestInterface
public Request::getQuery(): string
public Request::getProtocolVersion(): string
public Request::withProtocolVersion($version): MessageInterface
public Request::getHeaders(): array
public Request::hasHeader($name): bool
public Request::getHeader($name): array
public Request::getHeaderLine($name): string
public Request::withHeader($name, $value): MessageInterface
public Request::withAddedHeader($name, $value): MessageInterface
public Request::withoutHeader($name): MessageInterface
public Request::getBody(): StreamInterface
public Request::withBody($body): MessageInterface
}Methods#
| Method | Description |
|---|---|
__construct | Creates a request. |
__clone | Clones the URI set with withUri(), so a copy does not share it. |
create | Creates a request from a URI, deriving the request-target and Host header from it. |
getRequestTarget | Returns the request target of the message. |
withRequestTarget | Return an instance with the specific request-target. |
getMethod | Retrieves the HTTP method of the request. |
withMethod | Return an instance with the provided HTTP method. |
getUri | Returns the attached URI override, or a URI derived from the request-target plus the Host header. |
withUri | Returns an instance with the provided URI. |
getQuery | The query string from the request target, or from the URI override when one is attached. |
getProtocolVersion | Retrieves the HTTP protocol version as a string. |
withProtocolVersion | Return an instance with the specified HTTP protocol version. |
getHeaders | Returns the message headers. |
hasHeader | Checks if a header exists by the given case-insensitive name. |
getHeader | Retrieves a message header value by the given case-insensitive name. |
getHeaderLine | Retrieves a comma-separated string of the values for a single header. |
withHeader | Return an instance with the provided value replacing the specified header. |
withAddedHeader | Return an instance with the specified header appended with the given value. |
withoutHeader | Return an instance without the specified header. |
getBody | Gets the body of the message. |
withBody | Return an instance with the specified message body. |
Examples#
$request = new Request('POST', '/search?q=phasync', new StringStream('{"limit":10}'), [
'Host' => 'example.com',
'Content-Type' => 'application/json',
]);
(string) $request->getUri(); // "http://example.com/search?q=phasync"See also#
ServerRequestA PSR-7 request as a server received it.ResponseA PSR-7 response.