php-http/guzzle6-adapter
PSR-7/PSR-18 compatible adapter that lets you use Guzzle 6 as an HTTPlug HTTP client. Provides a bridge for sending requests through Guzzle while working with php-http contracts, useful for libraries that depend on standardized HTTP interfaces.
Installation Add the package via Composer:
composer require php-http/guzzle6-adapter
Require the Http\Adapter\Guzzle6\Client in your Laravel project:
composer require php-http/guzzle6-adapter guzzlehttp/guzzle
Basic Usage
Register the Guzzle6 adapter in Laravel's HTTP client configuration (config/http.php):
'plugins' => [
\Http\Adapter\Guzzle6\Client::class,
],
First Use Case Fetch data from an external API:
use Http\Client\Common\Plugin\AddHostPlugin;
use Http\Client\Common\Plugin\UrlEncodePlugin;
use Http\Client\Common\PluginClient;
use Http\Message\Authentication\Bearer\BearerAuth;
$client = new PluginClient(
new \Http\Adapter\Guzzle6\Client(),
[
new AddHostPlugin(),
new UrlEncodePlugin(),
]
);
$response = $client->get('https://api.example.com/data', [
'auth' => new BearerAuth('your-token-here'),
]);
$data = json_decode($response->getBody(), true);
Laravel HTTP Client Integration Use Laravel's built-in HTTP client with Guzzle6 adapter:
$response = Http::withOptions(['debug' => true])
->withHeaders(['Authorization' => 'Bearer token'])
->get('https://api.example.com/endpoint');
Middleware/Plugins Leverage Guzzle middleware (e.g., retry, logging):
use GuzzleHttp\Middleware;
$stack = \Http\Promise\Promise::wrap(function () use ($client) {
return $client->sendAsync(function ($request) {
return $request->getBody()->write('Hello, World!');
});
});
$stack->then(function ($response) {
echo $response->getBody();
});
Streaming Responses Handle large responses efficiently:
$response = $client->get('https://api.example.com/large-file');
$stream = $response->getBody();
while (!$stream->eof()) {
echo $stream->read(1024);
}
Http facade for simplicity.$this->app->bind(\Http\Client\Common\PluginClient::class, function ($app) {
return new PluginClient(
new \Http\Adapter\Guzzle6\Client(),
[$app->make(\Http\Client\Common\Plugin\BaseUrlPlugin::class)]
);
});
// config/guzzle.php
'default' => [
'timeout' => 10.0,
'verify' => false, // Disable SSL verification (not recommended for production)
],
Deprecated Guzzle6
php-http/guzzle7-adapter for new projects.SSL Verification
Disabling SSL verification (verify => false) is unsafe. Use:
'verify' => env('APP_ENV') !== 'production' ? false : __DIR__.'/path/to/cert.pem',
Stream Handling Guzzle6 streams behave differently than Guzzle7. Ensure proper resource cleanup:
$stream = $response->getBody();
$stream->rewind();
$data = $stream->getContents();
$stream->close();
$client = new \Http\Adapter\Guzzle6\Client([
'debug' => fopen('guzzle.log', 'w'),
]);
CurlException: Check allow_redirects, timeout, and SSL settings.ConnectionException: Verify the host/port and firewall rules.Custom Middleware
Add Guzzle middleware via Http\Client\Common\PluginClient:
use GuzzleHttp\Middleware;
$client = new PluginClient(
new \Http\Adapter\Guzzle6\Client(),
[
Middleware::tap(function ($request) {
$request->getHeaders()->add('X-Custom-Header', 'value');
}),
]
);
Retry Logic
Use php-http/retry-plugin for automatic retries:
use Http\Client\Common\Plugin\RetryPlugin;
$client = new PluginClient(
new \Http\Adapter\Guzzle6\Client(),
[new RetryPlugin()]
);
Mocking for Tests
Use GuzzleHttp\Handler\MockHandler:
$mock = new MockHandler([new Response(200, [], 'Mocked response')]);
$handler = new \GuzzleHttp\HandlerStack($mock);
$client = new \Http\Adapter\Guzzle6\Client(['handler' => $handler]);
How can I help you explore Laravel packages today?