graham-campbell/guzzle-factory
Simple factory for creating Guzzle HTTP clients with sensible defaults. One-liner client creation via GuzzleFactory::make(), with optional config like base_uri. Supports PHP 7.4–8.5 and integrates cleanly in modern PHP/Laravel apps.
Install the package:
composer require graham-campbell/guzzle-factory
Register the factory in config/app.php under providers:
GrahamCampbell\GuzzleFactory\GuzzleFactoryServiceProvider::class,
Publish the config (optional but recommended for customization):
php artisan vendor:publish --provider="GrahamCampbell\GuzzleFactory\GuzzleFactoryServiceProvider" --tag="config"
This creates config/guzzle-factory.php with sensible defaults.
First use case: Create a client in a controller or service:
use GrahamCampbell\GuzzleFactory\Facades\GuzzleFactory;
$client = GuzzleFactory::make(['base_uri' => 'https://api.example.com']);
GuzzleFactory::make() for quick client creation.$this->app->singleton('guzzle.example', fn() => GuzzleFactory::make(['base_uri' => 'https://example.com']));
config/guzzle-factory.php (e.g., timeout, retries, TLS settings).// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('stripe.client', fn() =>
GuzzleFactory::make(['base_uri' => 'https://api.stripe.com'])
);
}
stripe.client into services/controllers via constructor injection.config/services.php:
'services' => [
'stripe' => [
'base_uri' => env('STRIPE_API_URL'),
'timeout' => 10,
],
'paypal' => [
'base_uri' => env('PAYPAL_API_URL'),
'headers' => ['Accept' => 'application/json'],
],
],
$client = GuzzleFactory::make(config('services.stripe'));
$client = GuzzleFactory::make(
['base_uri' => 'https://api.example.com'],
null,
static function (HandlerStack $stack) {
$stack->push(
Middleware::tap(static function ($request) {
$request = $request->withHeader('X-Custom-Header', 'value');
return $request;
})
);
}
);
$client = GuzzleFactory::make(
['base_uri' => 'https://api.example.com'],
TransportSharing::HANDLER_PREFER // Best-effort sharing
);
$this->app->instance('guzzle.example', Mockery::mock(GuzzleHttp\Client::class));
$this->app->bind('api.client', fn() =>
GuzzleFactory::make(['base_uri' => config('services.api.url')])
);
php artisan config:cache) to optimize performance for production..env and reference them in the config file:
'services' => [
'stripe' => [
'base_uri' => env('STRIPE_API_URL'),
'auth' => ['username' => env('STRIPE_KEY'), 'password' => ''],
],
],
Custom Handler Stacks: Extend the factory to support custom middleware or plugins:
GuzzleFactory::extend('custom', function ($config) {
$stack = HandlerStack::create();
$stack->push(new CustomMiddleware());
return new GuzzleHttp\Client($config + ['handler' => $stack]);
});
Use the extended factory:
$client = GuzzleFactory::make(['base_uri' => 'https://example.com'], null, null, 'custom');
Retry Configuration: Customize retry logic via the config file:
'guzzle-factory' => [
'defaults' => [
'timeout' => 10,
'retries' => [
'max_retries' => 3,
'retry_delay' => 100,
'except' => [401, 403],
],
],
],
Handler Stack Misconfiguration
$stack->push(Middleware::class, 'name'); // Named middleware for clarity
Transport Sharing Overhead
TransportSharing::HANDLER_PREFER sparingly—it’s best-effort and may not always improve performance.$client = GuzzleFactory::make(['base_uri' => 'https://example.com']); // Default: no sharing
TLS/SSL Errors
verify settings.'guzzle-factory' => [
'defaults' => [
'verify' => true, // Always verify unless testing
'curl' => [
CURLOPT_SSL_VERSION => CURL_SSL_VERS_TLSv1_2,
],
],
],
openssl extension or use a custom CA bundle:
$client = GuzzleFactory::make([
'base_uri' => 'https://example.com',
'verify' => '/path/to/custom/ca-bundle.crt',
]);
Config Overrides
config/guzzle-factory.php) and ensure it’s published.php artisan vendor:publish --provider="GrahamCampbell\GuzzleFactory\GuzzleFactoryServiceProvider" --tag="config"
Dependency Injection Conflicts
Http facade or other Guzzle instances causing conflicts.$this->app->bind('custom.guzzle', fn() => GuzzleFactory::make([]));
Http facade and GuzzleFactory for the same API.Custom Factory Extensions
stripe, paypal).GuzzleFactory::extend('stripe', function ($config) {
return GuzzleFactory::make(
$config + ['auth' => ['username' => env('STRIPE_KEY'), 'password' => '']]
);
});
$stripe = GuzzleFactory::make([], null, null, 'stripe');
Dynamic Config Loading
$config = Cache::remember('api_config', 60, function () {
return ApiConfig::where('name', 'stripe')->first()->toArray();
});
$client = GuzzleFactory::make($config);
**Event-D
How can I help you explore Laravel packages today?