Installation:
composer require ddeboer/guzzle-bundle
(Note: The package is archived, but still functional. Prefer modern alternatives like nelmio/api-client-bundle or php-http/guzzle-adapter for new projects.)
Enable the Bundle in config/bundles.php:
return [
// ...
Ddeboer\GuzzleBundle\DdeboerGuzzleBundle::class => ['all' => true],
];
Basic Configuration (config/packages/ddeboer_guzzle.yaml):
ddeboer_guzzle:
clients:
api:
config:
base_url: 'https://api.example.com'
timeout: 30
First Use Case: Inject the client in a service/controller:
use Ddeboer\GuzzleBundle\Client\ClientInterface;
class MyController extends AbstractController {
public function __construct(private ClientInterface $client) {}
public function fetchData() {
$response = $this->client->get('api', '/endpoint');
return $this->json($response->json());
}
}
Service Integration: Create a dedicated service for API interactions:
class ApiService {
public function __construct(private ClientInterface $client) {}
public function getUser(int $id): array {
$response = $this->client->get('api', "/users/{$id}");
return $response->json();
}
}
Request Customization: Use middleware or request options:
$response = $this->client->get('api', '/search', [
'query' => ['q' => 'laravel'],
'headers' => ['Accept' => 'application/json'],
]);
Authentication:
Configure auth in config/packages/ddeboer_guzzle.yaml:
clients:
api:
config:
base_url: 'https://api.example.com'
auth: ['username', 'password'] # Basic Auth
Or use middleware for token-based auth:
$this->client->get('api', '/protected', [
'auth' => ['token', '']
]);
Response Handling: Streamline responses with custom handlers:
$response = $this->client->get('api', '/data');
$data = json_decode($response->getBody(), true);
config/packages/ddeboer_guzzle.yaml.GuzzleException:
try {
$response = $this->client->get('api', '/endpoint');
} catch (GuzzleException $e) {
// Log or rethrow
}
Deprecated Package:
nelmio/api-client-bundle) offer better support.getBody() → getBody()->getContents()).Configuration Overrides:
yaml are merged; later definitions override earlier ones.$this->client->createClient('dynamic', [
'base_url' => 'https://dynamic.example.com',
]);
Middleware Conflicts:
Guzzle\Common\Middleware. Example:
use Guzzle\Common\Middleware;
class AddHeaderMiddleware implements Middleware {
public function __invoke(callable $handler) {
return function ($request) use ($handler) {
$request->addHeader('X-Custom', 'Header');
return $handler($request);
};
}
}
Register in config/packages/ddeboer_guzzle.yaml:
clients:
api:
middleware: ['add_header']
Caching Responses:
use Guzzle\Plugin\Cache\CachePlugin;
use Guzzle\Plugin\Cache\FileCacheAdapter;
$cache = new FileCacheAdapter('/path/to/cache');
$this->client->addSubscriber(new CachePlugin($cache));
Enable Guzzle Debugging:
Add to config/packages/ddeboer_guzzle.yaml:
clients:
api:
config:
debug: true
(Logs requests/responses to var/log/guzzle.log.)
Inspect Requests:
Use Guzzle\Service\Description\ServiceDescription for API discovery:
$description = new ServiceDescription('https://api.example.com/api.xml');
$this->client->setDescription($description);
Common Issues:
verify in client config:
clients:
api:
config:
verify: /path/to/cert.pem
timeout in config or per-request:
$this->client->get('api', '/slow', ['timeout' => 60]);
Custom Clients: Dynamically create clients in services:
$client = $this->client->createClient('third_party', [
'base_url' => 'https://thirdparty.com',
]);
Event Subscribers:
Subscribe to Guzzle events (e.g., request.before_send):
use Guzzle\Event\EventInterface;
class MySubscriber implements SubscriberInterface {
public function getEvents() {
return ['request.before_send' => 'onBeforeSend'];
}
public function onBeforeSend(EventInterface $event) {
$request = $event->getRequest();
$request->addHeader('X-Request-ID', uniqid());
}
}
Register in config/packages/ddeboer_guzzle.yaml:
clients:
api:
subscribers: ['my_subscriber']
PSR-7 Compatibility:
Use GuzzleHttp\Psr7 for PSR-7 requests/responses if needed:
use GuzzleHttp\Psr7\Request;
$request = new Request('GET', '/endpoint');
$response = $this->client->send($request);
How can I help you explore Laravel packages today?