## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require eightpoints/guzzle-bundle
Symfony Flex automatically registers the bundle. For non-Flex projects, add to config/bundles.php:
EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle::class => ['all' => true],
Configure a Client:
Create config/packages/eight_points_guzzle.yaml:
eight_points_guzzle:
clients:
api:
base_url: 'https://api.example.com'
options:
headers:
Accept: 'application/json'
First Use Case: Inject the client into a service/controller:
use Psr\Http\Client\ClientInterface;
class MyService {
public function __construct(private ClientInterface $apiClient) {}
public function fetchData() {
$response = $this->apiClient->request('GET', '/endpoint');
return json_decode($response->getBody(), true);
}
}
eight_points_guzzle.yaml for different APIs (e.g., payment, crm).lazy: true for clients initialized on-demand (reduces startup overhead):
clients:
payment:
lazy: true
base_url: '%env(API_URL)%'
ClientInterface) or alias:
public function __construct(private ClientInterface $apiClient) {}
services:
App\Service\PaymentService:
arguments:
$client: '@eight_points_guzzle.client.payment'
GuzzleHttp\Psr7\Request for pre-built requests:
$request = new Request('POST', '/orders', [], json_encode($data));
$response = $this->apiClient->send($request);
$promises = [
$this->apiClient->requestAsync('GET', '/orders'),
$this->apiClient->requestAsync('GET', '/users'),
];
$responses = \GuzzleHttp\Promise\Utils::settle($promises)->wait();
Kernel.php:
yield new EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle([
new \Gregurco\Bundle\GuzzleBundleOAuth2Plugin\GuzzleBundleOAuth2Plugin(),
]);
clients:
api:
plugin:
retry:
max_retries: 3
delay: 100
services:
App\EventListener\GuzzleListener:
tags:
- { name: 'kernel.event_listener', event: 'eight_points_guzzle.pre_transaction.api', method: 'onPreRequest' }
public function onPreRequest(PreTransactionEvent $event) {
$event->getRequest()->setHeader('X-Custom-Header', 'value');
}
Curl Options:
CURLOPT_* to lowercase (e.g., sslversion instead of CURLOPT_SSLVERSION).options:
curl:
sslversion: 6 # TLS 1.2
guzzlehttp/guzzle:^7.0 with --verbose flag to inspect raw requests.Lazy Loading:
lazy: true) are initialized on first use. Avoid circular dependencies in DI.Plugin Conflicts:
gregurco/GuzzleBundleOAuth2Plugin may need updates).Symfony Profiler:
Logging:
[{datetime}] eight_points_guzzle.{level}: {method} {uri} {code}.eight_points_guzzle.symfony_log_formatter.pattern in config:
eight_points_guzzle:
symfony_log_formatter:
pattern: '[Guzzle] {method} {uri} ({code})'
Slow Responses:
slow_response_time (ms) to log slow requests:
eight_points_guzzle:
slow_response_time: 1000 # Log responses >1s
Connection Pooling:
Caching:
gregurco/GuzzleBundleCachePlugin for response caching:
clients:
api:
plugin:
cache:
adapter: 'cache.app'
ttl: 300
Error Handling:
clients:
api:
options:
exceptions: false
try-catch with GuzzleException for granular control.Custom Client Class:
GuzzleHttp\Client by configuring:
eight_points_guzzle:
client_class: 'App\Service\CustomGuzzleClient'
Environment Variables:
%env%:
options:
auth:
- '%env(API_USERNAME)%'
- '%env(API_PASSWORD)%'
Single-File Plugins:
// src/Plugin/CustomHeaderPlugin.php
use EightPoints\Bundle\GuzzleBundle\Plugin\PluginInterface;
class CustomHeaderPlugin implements PluginInterface {
public function apply(ClientBuilder $builder) {
$builder->getHandlerStack()->push(
Middleware::tap(function ($request) {
$request = $request->withHeader('X-Custom', 'value');
return $request;
})
);
}
}
Kernel.php:
yield new EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle([
new CustomHeaderPlugin(),
]);
SSL Errors:
options:
curl:
verify: false
Circular Dependencies:
Plugin Loading Order:
Symfony 7+ Deprecations:
Throwable instead of Exception in event listeners (v8.5.0+).
---
How can I help you explore Laravel packages today?