HttpClient

HttpClient implements PSR-18's ClientInterface — like Cache, it's instantiated rather than called statically (see Service Container).

Construction

new HttpClient(
    int $timeoutSeconds = 30,
    int $connectTimeoutSeconds = 10,
    bool $verifySsl = true,
    int $maxRedirects = 5,
)

Methods

$client->sendRequest(Psr\Http\Message\RequestInterface $request): ResponseInterface // the PSR-18 method
$client->request(string $method, string $uri, array $headers = [], string $body = ''): ResponseInterface
$client->get(string $uri, array $headers = []): ResponseInterface
$client->post(string $uri, string $body = '', array $headers = []): ResponseInterface
$client->put(string $uri, string $body = '', array $headers = []): ResponseInterface
$client->patch(string $uri, string $body = '', array $headers = []): ResponseInterface
$client->delete(string $uri, array $headers = []): ResponseInterface
$client->postJson(string $uri, mixed $data, array $headers = []): ResponseInterface // sets Content-Type: application/json
$client->withTimeoutSeconds(int $timeoutSeconds): static // returns a new instance with a different timeout
$client = new HttpClient();
$response = $client->postJson('https://api.example.com/webhooks', ['event' => 'order.created']);
$response->getStatusCode();

Throws HttpClientException on DNS failure, connection refusal, or timeout.

Next steps

  • LLM — the main built-in consumer of HttpClient.
  • PSR-7 BridgeHttpClient speaks PSR-7 requests/responses natively.