HttpClient Usage
Start by choosing whether you need one request or many.
Send a single request
use Quantum\HttpClient\HttpClient;
$client = (new HttpClient())
->createRequest('https://api.example.com/users')
->setMethod('GET')
->setHeader('Accept', 'application/json')
->start();
$body = $client->getResponseBody();
$headers = $client->getResponseHeaders();
$status = $client->info(CURLINFO_RESPONSE_CODE);
Because response headers are normalized to lowercase, read them like this:
$contentType = $client->getResponseHeaders('content-type');
Send JSON or form data in a single request
$client = (new HttpClient())
->createRequest('https://api.example.com/users')
->setMethod('POST')
->setHeader('Content-Type', 'application/json')
->setData(['name' => 'Arman', 'role' => 'admin'])
->start();
setData() is passed through curl-class buildPostData(...) before the request is executed.
Check transport errors
$errors = $client->getErrors();
if (!empty($errors)) {
$code = $errors['code'];
$message = $errors['message'];
}
The package records transport-layer curl errors. It does not treat HTTP 4xx or 5xx responses as package exceptions by itself.
Queue multiple requests
use Quantum\HttpClient\HttpClient;
$client = (new HttpClient())->createMultiRequest();
$client->addGet('https://api.example.com/users');
$client->addGet('https://api.example.com/teams');
$client->start();
$responses = $client->getResponse();
$errors = $client->getErrors();
In multi mode, $responses is keyed by curl request ID. Inspect each entry's headers, cookies, and body sections.
Use async callbacks for multi requests
use Curl\Curl;
use Quantum\HttpClient\HttpClient;
$client = (new HttpClient())->createAsyncMultiRequest(
function (Curl $request) {
$body = $request->getResponse();
},
function (Curl $request) {
$error = $request->getErrorMessage();
}
);
$client->addGet('https://api.example.com/users');
$client->start();
In this mode, handle the result inside your callbacks. Do not rely on getResponse() or getErrors() being populated afterward.
Recommended usage patterns
- use single-request mode when you need response inspection helpers like
info()orgetResponseBody() - use normal multi-request mode when you want Quantum to collect finished responses for later inspection
- use async multi-request mode only when callback handling is the real output path
- set headers through
setHeader()orsetHeaders()if you wantgetRequestHeaders()to reflect them later - prefer a fresh
HttpClientinstance for each independent request or batch
Caveats
- call
createRequest()or one of the multi-request builders before any proxied curl-class method setMethod()only affects the single-request execution pathsetData()is skipped when the value is empty or otherwise falsey- recreating the underlying request does not clear earlier wrapper state; if you reuse one instance, reset or overwrite method, data, and headers deliberately
- multi-request response and error collections stay on the wrapper object until you discard that instance
- single-request getters throw for multi clients instead of choosing one request implicitly