Installation:
composer require widop/http-adapter-bundle
Enable the bundle in config/bundles.php:
Widop\HttpAdapterBundle\WidopHttpAdapterBundle::class => ['all' => true],
Configuration:
Configure the default adapter in config/packages/widop_http_adapter.yaml:
widop_http_adapter:
adapter: guzzle # or 'curl', 'stream', 'buzz', 'zend'
options: ~
First Request:
Inject the Widop\HttpAdapterBundle\Client service and make a request:
use Widop\HttpAdapterBundle\Client;
class MyService
{
public function __construct(private Client $httpClient) {}
public function fetchData()
{
$response = $this->httpClient->get('https://api.example.com/data');
return json_decode($response->getBody(), true);
}
}
Client service: Central interface for HTTP requests.Request builder: Chainable methods (get(), post(), put(), etc.) with options.$response->getBody(), $response->getStatusCode(), etc.// GET request with query params
$response = $this->httpClient->get('https://api.example.com/users', [
'query' => ['id' => 123]
]);
// POST with JSON body
$response = $this->httpClient->post('https://api.example.com/users', [
'json' => ['name' => 'John Doe']
]);
$this->httpClient->get('https://api.example.com/protected', [
'auth' => ['username', 'password']
]);
$this->httpClient->get('https://api.example.com/protected', [
'headers' => ['Authorization' => 'Bearer token123']
]);
Use Symfony’s HttpClient middleware or bundle-specific interceptors:
# config/packages/widop_http_adapter.yaml
widop_http_adapter:
adapter: guzzle
options:
middleware:
- Widop\HttpAdapterBundle\Middleware\LoggingMiddleware
- Widop\HttpAdapterBundle\Middleware\RetryMiddleware
widop_http_adapter:
adapter: curl
options:
curl: [CURLOPT_TIMEOUT, 30]
$response = $this->httpClient->get('https://large-file.example.com', [
'stream' => true
]);
Bind custom clients or adapters:
// src/Service/MyCustomClient.php
class MyCustomClient extends Widop\HttpAdapterBundle\Client
{
public function customRequest()
{
return $this->request('GET', 'https://custom.example.com', [
'headers' => ['X-Custom-Header' => 'value']
]);
}
}
Adapter Compatibility:
stream lacks advanced auth).guzzle for broad compatibility.Response Handling:
$response->getStatusCode() before accessing $response->getBody().
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException('API Error: ' . $response->getBody());
}
stream: true may buffer large responses in memory.Configuration Overrides:
config/packages/ takes precedence over config/bundles.php defaults.Middleware Order:
widop_http_adapter:
adapter: guzzle
options:
debug: true # Logs full request/response
CURLOPT_VERBOSE via config.GuzzleHttp\HandlerStack debugging.Custom Adapters:
Implement Widop\HttpAdapter\AdapterInterface and register via DI:
services:
app.custom_adapter:
class: App\Adapter\MyAdapter
tags: ['widop.http_adapter.adapter']
Response Transformers:
Extend Widop\HttpAdapterBundle\Response to add domain-specific methods:
class ApiResponse extends Widop\HttpAdapterBundle\Response
{
public function asJson(): array
{
return json_decode($this->getBody(), true);
}
}
Event Listeners:
Subscribe to widop.http_adapter.request and widop.http_adapter.response events for cross-cutting concerns (e.g., rate limiting).
guzzle or buzzle for HTTP/2 and connection reuse.widop_http_adapter:
options:
middleware:
- Widop\HttpAdapterBundle\Middleware\RetryMiddleware:
max_retries: 3
delay: 100
HttpCache or CacheAdapter for responses.options: ~ if no adapter-specific config is needed.curl: Use curl: [option, value].guzzle: Use guzzle: [handler, config].widop_http_adapter:
options:
headers:
Accept: application/json
User-Agent: MyApp/1.0
How can I help you explore Laravel packages today?