symfony/http-client
Symfony HttpClient provides a modern API to fetch HTTP resources synchronously or asynchronously. Supports efficient streaming, retries, and multiple transports, making it easy to integrate robust HTTP requests into Symfony or any PHP app.
symfony/http-client package is a drop-in replacement for Laravel’s native HTTP client (Illuminate\Support\Facades\Http) or Guzzle-based solutions. It aligns with Laravel’s ecosystem (Symfony components are first-party dependencies) and offers decorator-based middleware, async support, and caching—features that can modernize legacy HTTP layers.AsyncResponse (critical for high-concurrency APIs).CachingHttpClient with RFC 9111 compliance (reduces redundant API calls).StreamedResponse.HttpClientInterface binding).AsyncResponse + queue: middleware).CachingHttpClient.Http::asGuzzle() calls with minimal code changes (use HttpClient + Psr18Client decorator).Http facade with a custom facade class (e.g., HttpClientFacade).RetryMiddleware) can be adapted to Symfony’s decorator pattern.AsyncResponse for background HTTP tasks (e.g., webhook processing).Http::post()->toQueue() with AsyncResponse + dispatch().Cache::remember) with CachingHttpClient + Laravel’s cache drivers.| Risk Area | Mitigation Strategy |
|---|---|
| Async Complexity | Start with synchronous requests; gradually adopt async via AsyncResponse. |
| Middleware Migration | Use adapter classes to translate Guzzle middleware to Symfony decorators. |
| Performance Overhead | Benchmark CachingHttpClient vs. manual caching; monitor memory usage. |
| PHP Version | Ensure PHP ≥8.1 (required for Symfony 7/8); use Psr18Client for broader compatibility. |
| Streaming Edge Cases | Test with large responses (e.g., video downloads) to validate StreamedResponse. |
| Dependency Bloat | Audit symfony/http-client dependencies (e.g., symfony/cache for caching). |
AsyncResponse directly?CachingHttpClient replace Laravel’s cache layer entirely, or coexist?HttpClientInterface to HttpClient in AppServiceProvider.Http facade or create HttpClient facade.CurlHttpClient (most stable, feature-complete).AsyncHttpClient (requires AMP or ReactPHP; test performance impact).Psr18Client for vendor-agnostic integrations (e.g., with nyholm/psr7).RetryDecorator, AuthDecorator).| Phase | Action Items | Tools/Examples |
|---|---|---|
| Assessment | Audit HTTP calls in codebase; categorize by complexity (simple GETs vs. async streams). | grep -r "Http::" app/ + phpstan analysis. |
| Facade Layer | Create HttpClient facade wrapping symfony/http-client. |
php<br>class HttpClient extends Facade { protected static function getFacadeAccessor() { return 'http.client'; } }<br> |
| Synchronous Swap | Replace Http::get() → app('http.client')->request('GET', ...). |
Use Psr18Client for PSR-18 compatibility. |
| Middleware | Migrate Guzzle middleware to Symfony decorators. | Example: Convert GuzzleRetryMiddleware → RetryDecorator. |
| Async Adoption | Replace Http::post()->toQueue() with AsyncResponse + Laravel queues. |
php<br>$response = $client->request('POST', ...)->getAsync();<br>$response->then(fn($res) => dispatch(new ProcessWebhook($res)));<br> |
| Caching | Replace manual caching with CachingHttpClient + Laravel cache drivers. |
php<br>$client = new CachingHttpClient($client, new Psr6CacheAdapter());<br> |
| Testing | Update HTTP tests to use HttpClient mocks. |
Use MockHttpClient or MockResponse. |
HttplugClient decorator to wrap Guzzle instances temporarily.HandleWebhook).HttpClientEvent (custom) or leverage Symfony’s HttpClientEvent.ValidatedRequest with Symfony’s RequestOptions.AsyncResponse.CachingHttpClient for high-frequency API calls.config/http.php).AsyncResponse::wait() for debugging).Monolog via HttpClientEventListener.HttpClient events (latency, retries) with Laravel Scout or Prometheus.timeout option or use RetryDecorator.AsyncResponse is properly awaited or queued.CachePool configuration.HttpClient::withOptions(['debug' => true]) forHow can I help you explore Laravel packages today?