guzzle/http
Legacy Guzzle HTTP component providing request/response objects, message abstractions, and client utilities for making HTTP calls in PHP. Useful for older Guzzle integrations and compatibility layers; for new projects, prefer modern guzzlehttp/guzzle versions.
Installation
composer require guzzlehttp/guzzle
(Note: The package mentioned is a legacy reference to Guzzle 3, but the modern equivalent is guzzlehttp/guzzle for Guzzle 6/7.)
First Request
use GuzzleHttp\Client;
$client = new Client();
$response = $client->get('https://api.example.com/data');
$body = $response->getBody()->getContents();
Key Files
vendor/guzzlehttp/guzzle/src/ (Core classes)config/guzzle.php (If using Laravel’s HTTP client wrapper)Fetching JSON from an API
$response = $client->request('GET', 'https://api.example.com/users', [
'headers' => ['Accept' => 'application/json'],
]);
$data = json_decode($response->getBody(), true);
Reusable Clients
// In a service provider or config
$client = new Client([
'base_uri' => 'https://api.example.com/v1/',
'timeout' => 10.0,
'headers' => ['User-Agent' => 'MyApp/1.0'],
]);
Middleware for Logging/Modifying Requests
use GuzzleHttp\Middleware;
$stack = HandlerStack::create();
$stack->push(Middleware::tap(function ($request) {
// Log request details
logger()->debug('API Request:', ['url' => $request->getUri()]);
}));
$client = new Client(['handler' => $stack]);
Async Requests
$promises = [];
foreach ($urls as $url) {
$promises[] = $client->getAsync($url);
}
$results = GuzzleHttp\Promise\Utils::settle($promises)->wait();
Laravel Integration
// Using Laravel's HTTP facade (wraps Guzzle)
$response = Http::withOptions(['timeout' => 5])
->get('https://api.example.com/data');
Client instances for connection pooling (avoid recreating per request).RequestOptions for headers, auth, and timeouts.getBody(), getStatusCode(), or json() helper.$client->get('https://large-file.example.com', [
'sink' => 'file://local-path'
]);
Deprecations in Guzzle 7
getBody()->read() → Use getBody()->getContents().send() → Use request() or get()/post().createRequest() → Use createRequest() (still valid, but prefer request()).Timeout Handling
'timeout' => 5.0, // 5 seconds
'connect_timeout' => 2.0,
SSL Issues
'verify' => false, // ⚠️ Avoid in production
'verify' => '/path/to/cert.pem',
Memory Leaks
$response->getBody()->close();
Laravel-Specific Quirks
Http facade resets state between requests (unlike raw Guzzle).Http::macro('customClient', function () {
return new Client(['base_uri' => config('services.api.base_uri')]);
});
$stack->push(Middleware::history());
$history = $stack->getHandler()->getHistory();
if ($response->getStatusCode() === 401) {
// Handle unauthorized
}
$headers = $response->getHeaders();
GuzzleHttp\Handler\HandlerInterface for proxy logic.GuzzleHttp\Plugin\PluginInterface to add features (e.g., retry logic).GuzzleHttp\Psr7 classes for custom requests/responses.$client->getEmitter()->attach(
'request.error',
function ($event) { /* Handle errors */ }
);
How can I help you explore Laravel packages today?