HttpClient Usage
Start by choosing whether you need one request or many.
Send a single request
$client = httpRequest('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');A JSON or XML response body arrives already decoded; see the response model.
Send JSON or form data in a single request
$client = httpRequest('https://api.example.com/users')
->setMethod('POST')
->setHeader('Content-Type', 'application/json')
->setData(['name' => 'Arman', 'role' => 'admin'])
->start();How setData() is sent depends on the Content-Type header you set before start():
- an array (or a
JsonSerializable) with anapplication/jsonorapplication/...+jsonheader is sent as a JSON body - an array without a JSON header is form-encoded, like
name=Arman&role=admin - an array with a
multipart/form-dataheader, or one that holds aCURLFile, is sent as multipart, which is how you upload a file - a string is sent as it is
$client = httpRequest('https://api.example.com/avatar')
->setMethod('POST')
->setData(['user' => 'arman', 'avatar' => new CURLFile('/path/to/avatar.png')])
->start();Check errors
$errors = $client->getErrors();
if (!empty($errors)) {
$code = $errors['code'];
$message = $errors['message'];
}getErrors() is empty when the request worked. It holds an entry when cURL failed to complete the request, and also when the server answered with an HTTP 4xx or 5xx status. A request to a missing page gives:
['code' => 404, 'message' => 'HTTP/1.1 404 Not Found']For a cURL failure, code is the cURL error number and message is its description. Neither case throws an exception. The response body of a 4xx or 5xx answer is still available through getResponseBody().
Queue multiple requests
$client = httpMultiRequest();
$client->addGet('https://api.example.com/users', ['page' => 2]);
$client->addGet('https://api.example.com/teams');
$client->addPost('https://api.example.com/teams', ['name' => 'Core']);
$client->start();
$responses = $client->getResponse();
$errors = $client->getErrors();addGet() appends its data array to the URL as a query string, with & when the URL already has one, so the first request above asks for /users?page=2. addPost() builds its body the same way as setData().
In multi mode, $responses is keyed by curl request ID. Inspect each entry's headers, cookies, and body sections. $errors is keyed the same way and holds only the requests that failed.
Use async callbacks for multi requests
use Quantum\HttpClient\Contracts\CurlAdapterInterface;
$client = httpAsyncMultiRequest(
function (CurlAdapterInterface $request) {
$body = $request->getResponse();
},
function (CurlAdapterInterface $request) {
$error = $request->getErrorMessage();
}
);
$client->addGet('https://api.example.com/users');
$client->start();In this mode, handle each result inside your callbacks when you need per-request side effects. A request that failed, including one that got an HTTP 4xx or 5xx answer, goes to the error callback; every other request goes to the success callback. getResponse() and getErrors() are also populated after start() finishes.
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 when callback handling is needed during batch execution
- 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 adapter passthrough 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
