guzzlehttp/guzzle
Guzzle is a PHP HTTP client for sending sync or async requests with an easy API. Built on PSR-7 and PSR-18, supports middleware, cookies, streaming uploads/downloads, and JSON. Transport-agnostic for flexible integrations.
Install via Composer: composer require guzzlehttp/guzzle. Start with the GuzzleHttp\Client for basic HTTP interactions—synchronous or asynchronous. The simplest first use case: fetching JSON from an API endpoint.
use GuzzleHttp\Client;
$client = new Client();
$response = $client->get('https://api.example.com/users/123');
$data = json_decode($response->getBody(), true);
Key things to explore first:
getResponse(), getBody(), etc.).sendAsync() or requestAsync() for non-blocking operations (requires handling promises).$client = new Client([
'handler' => HandlerStack::create()->push(Middleware::retry($retryDecider))
]);
['json' => $payload], ['headers' => ['Authorization' => 'Bearer ...']], or ['sink' => $stream] to request(), get(), post(), etc.Promise\Utils::all() or GuzzleHttp\Pool for concurrent requests:
$pool = new Pool($client, $generators, [
'concurrency' => 5,
'fulfilled' => function ($response, $id) { /* handle */ },
'rejected' => function ($reason, $id) { /* handle */ },
]);
$promise = $pool->promise();
$promise->wait();
Http::client() (Symfony HttpKernel-based), which wraps guzzlehttp/guzzle. In tests, mock responses via MockHandler and middleware.push() wraps earlier ones.StringBody, wrap in Stream::factory($content)—it’s rewindable by default.connect_timeout (DNS + TCP handshake) and timeout (total request duration) are distinct—configure both explicitly for reliability.Pool or utils::unwrap() with a handler stack supporting concurrency (e.g., cURL multi).curl.debug or debug handler option to see raw HTTP. In Laravel, use dd($response->getStatusCode(), $response->getHeaders()).GuzzleHttp\Client with base_uri and inject config via dependency injection rather than hardcoding URLs.ClientInterface from psr/http-client is fully supported—great for decoupling from Guzzle-specific APIs in public interfaces.How can I help you explore Laravel packages today?