phasync
HTTP client / Request

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#

MethodDescription
__constructCreates a request.
__cloneClones the URI set with withUri(), so a copy does not share it.
createCreates a request from a URI, deriving the request-target and Host header from it.
getRequestTargetReturns the request target of the message.
withRequestTargetReturn an instance with the specific request-target.
getMethodRetrieves the HTTP method of the request.
withMethodReturn an instance with the provided HTTP method.
getUriReturns the attached URI override, or a URI derived from the request-target plus the Host header.
withUriReturns an instance with the provided URI.
getQueryThe query string from the request target, or from the URI override when one is attached.
getProtocolVersionRetrieves the HTTP protocol version as a string.
withProtocolVersionReturn an instance with the specified HTTP protocol version.
getHeadersReturns the message headers.
hasHeaderChecks if a header exists by the given case-insensitive name.
getHeaderRetrieves a message header value by the given case-insensitive name.
getHeaderLineRetrieves a comma-separated string of the values for a single header.
withHeaderReturn an instance with the provided value replacing the specified header.
withAddedHeaderReturn an instance with the specified header appended with the given value.
withoutHeaderReturn an instance without the specified header.
getBodyGets the body of the message.
withBodyReturn an instance with the specified message body.

Examples#

Example 1
$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#