Skip to content

HttpClient

The framework HTTP client: a PSR-18 ClientInterface wrapping an underlying transport, adding base-URI resolution, default headers, a transient-failure retry policy, ergonomic verb helpers, and — the payoff for the whole abstraction — the central egress seam for telemetry.

Every outbound request opens a CLIENT-kind span and injects W3C traceparent so downstream services continue the trace (the outbound half of the request-tracing story, previously blocked on the absence of exactly this client).

Obtain instances via HttpClientFactory (memoized, named) rather than constructing directly, so a worker reuses one client per name for its lifetime instead of rebuilding on every call.

final class HttpClient implements ClientInterface

ImplementsClientInterface
SourceHttp/Client/HttpClient.php

public function __construct(ClientInterface $transport, string $baseUri = '', array $defaultHeaders = [], int $retries = 0, int $retryBaseDelayMs = 100, Psr17Factory $psr17 = new Psr17Factory(…)): mixed

ParameterTypeDescription
$transportClientInterface
$baseUristring
$defaultHeadersarray
$retriesint
$retryBaseDelayMsint
$psr17Psr17Factory

Returns mixed

MethodDescription
delete(string $uri, array{headers?: array<string, string>} $options = []): ResponseInterface
fromConfig(HttpClientConfig $config): HttpClientBuilds a client from a resolved HttpClientConfig, taking its transport, base URI, default headers and retry policy.
get(string $uri, array{headers?: array<string, string>} $options = []): ResponseInterface
post(string $uri, array{headers?: array<string, string>, body?: string} $options = []): ResponseInterface
put(string $uri, array{headers?: array<string, string>, body?: string} $options = []): ResponseInterface
request(string $method, string $uri, array{headers?: array<string, string>, body?: string} $options = []): ResponseInterfaceErgonomic request builder.
sendRequest(RequestInterface $request): ResponseInterfacePSR-18 entry point.

public function delete(string $uri, array{headers?: array<string, string>} $options = []): ResponseInterface

ParameterTypeDescription
$uristring
$optionsarray{headers?: array<string, string>}

Returns ResponseInterface

public static function fromConfig(HttpClientConfig $config): HttpClient

Builds a client from a resolved HttpClientConfig, taking its transport, base URI, default headers and retry policy.

The PSR-17 factory is not configurable this way: the client uses its own default.

ParameterTypeDescription
$configHttpClientConfig

Returns HttpClient

public function get(string $uri, array{headers?: array<string, string>} $options = []): ResponseInterface

ParameterTypeDescription
$uristring
$optionsarray{headers?: array<string, string>}

Returns ResponseInterface

public function post(string $uri, array{headers?: array<string, string>, body?: string} $options = []): ResponseInterface

ParameterTypeDescription
$uristring
$optionsarray{headers?: array<string, string>, body?: string}

Returns ResponseInterface

public function put(string $uri, array{headers?: array<string, string>, body?: string} $options = []): ResponseInterface

ParameterTypeDescription
$uristring
$optionsarray{headers?: array<string, string>, body?: string}

Returns ResponseInterface

public function request(string $method, string $uri, array{headers?: array<string, string>, body?: string} $options = []): ResponseInterface

Ergonomic request builder.

ParameterTypeDescription
$methodstring
$uristring
$optionsarray{headers?: array<string, string>, body?: string}

Returns ResponseInterface

public function sendRequest(RequestInterface $request): ResponseInterface

PSR-18 entry point.

Applies default headers, telemetry, and the retry policy.

ParameterTypeDescription
$requestRequestInterface

Returns ResponseInterface