digital-link/httpclient-buzz
Laravel-friendly integration for the Buzz HTTP client, providing a simple way to send HTTP requests and handle responses within your app or package. Useful for lightweight API calls, configurable clients, and swapping transports with minimal setup.
Installation Add the package via Composer:
composer require digital-link/httpclient-buzz
Register the service provider in config/app.php:
'providers' => [
// ...
DigitalLink\HttpClient\BuzzServiceProvider::class,
],
Basic Usage Resolve the client via Laravel’s IoC container:
$client = app('buzz.client');
Or inject it into a controller/service:
public function __construct(\Buzz\Client\ClientInterface $client) {
$this->client = $client;
}
First HTTP Request Use the client to make a GET request:
$response = $this->client->get('https://api.example.com/data');
$content = $response->getContent();
Request Customization Configure headers, timeouts, or authentication:
$request = $this->client->get('https://api.example.com/data', [
'headers' => ['Authorization' => 'Bearer token123'],
'timeout' => 10,
]);
Handling Responses Parse JSON responses or check status codes:
$response = $this->client->post('https://api.example.com/submit', [
'body' => json_encode(['key' => 'value']),
'headers' => ['Content-Type' => 'application/json'],
]);
if ($response->getStatusCode() === 200) {
$data = json_decode($response->getContent(), true);
}
Middleware Integration
Attach middleware (e.g., logging, retries) via the Buzz client:
$client = new \Buzz\Client\Client([
'plugins' => [
new \Buzz\Plugin\LoggerPlugin(),
new \Buzz\Plugin\RetryPlugin(),
],
]);
app()->bind('buzz.client', function () use ($client) {
return $client;
});
Dependency Injection Bind the client to interfaces for better testability:
$this->app->bind(
\Buzz\Client\ClientInterface::class,
\DigitalLink\HttpClient\BuzzClient::class
);
Deprecated Package
buzz/buzz v0.10+).No Built-in Laravel Facade
Http::buzz() facade; use app('buzz.client') directly or create a custom facade.Response Handling Quirks
getContent() returns raw strings; decode JSON manually:
json_decode($response->getContent(), true);
200), not Response objects.Configuration Overrides
Buzz\Client directly in the service provider:
$this->app->singleton('buzz.client', function () {
return new \Buzz\Client\Client([
'base_url' => 'https://api.example.com',
'timeout' => 30,
]);
});
Enable Buzz Logging
Attach a LoggerPlugin to inspect requests/responses:
$client->getPlugin('logger')->setOutput(fopen('php://stdout', 'w'));
Validate Headers Ensure headers are formatted as key-value arrays:
// Correct:
['headers' => ['Accept' => 'application/json']]
// Incorrect (will fail silently):
['headers' => 'application/json']
Timeout Handling Set timeouts explicitly to avoid hanging:
$client->setTimeout(15); // Global timeout (seconds)
Custom Request Factories
Extend the client to wrap requests in Laravel’s Illuminate\Http\Request:
$request = new \Illuminate\Http\Request([
'url' => 'https://api.example.com',
'method' => 'POST',
'headers' => ['X-Custom' => 'Header'],
]);
$response = $this->client->send($request);
Response Decorators Create a decorator to add Laravel-specific methods:
class LaravelBuzzResponse implements \Buzz\Client\ResponseInterface {
protected $response;
public function __construct(\Buzz\Client\ResponseInterface $response) {
$this->response = $response;
}
public function json() {
return json_decode($this->response->getContent(), true);
}
}
How can I help you explore Laravel packages today?