Docs LATEST

HTTP Client

VelvetCMS cURL-backed HTTP client (requests, retries, and structured option keys).

Services

Namespace: VelvetCMS\Http\Client\HttpClient

The container binds a shared instance with defaults (HttpClient::__construct('', [], 5, 30)).

All verbs delegate to send(string $method, string $url, array $options = []): HttpResponse.

Verb shortcuts #

use VelvetCMS\Http\Client\HttpClient;

$client = app(HttpClient::class);

$response = $client->get('https://example.com/status', [
    'query' => ['verbose' => 1],
    'headers' => ['Accept' => 'application/json'],
]);

$response = $client->post('https://example.com/tokens', [
    'json' => ['client_id' => 'cms'],
]);

Implemented: get, post, put, patch, delete, arbitrary methods via send.

Important option keys #

Key Purpose
query Associative array merged into the URL query string
headers string => string map merged before the request
json Encodable payload: builds JSON body and sets Content-Type
form application/x-www-form-urlencoded body
body Raw string payload
bearer Adds Authorization: Bearer …
auth [$username, $password] for Basic auth
connectTimeout, timeout Override per-call (constructor defaults remain 5s / 30s)

Wrap query parameters instead of passing them as a bare second argument:

$client->get('/search', ['query' => ['q' => 'velvet']]);

Response object #

VelvetCMS\Http\Client\HttpResponse provides status(), body(), json(), header(), headers(), successful() / ok(), clientError(), serverError(), failed(), and throw() (throws HttpRequestException when failed()).

Retries #

$response = $client->retry('GET', $url, times: 3, delayMs: 100, options: [
    'query' => ['probe' => 1],
]);

Retries on transport errors and 5xx responses with exponential backoff on the delay.