larafony / http-guzzle
Guzzle HTTP client bridge for Larafony Framework
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/larafony/http-guzzle
Requires
- php: ^8.5
- guzzlehttp/guzzle: ^7.9
- larafony/core: ^2.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.4
README
This package provides integration between Larafony Framework and Guzzle - the most popular PHP HTTP client.
Installation
composer require larafony/http-guzzle
Usage
Register the service provider in your bootstrap.php:
use Larafony\Http\Guzzle\ServiceProviders\GuzzleServiceProvider; $app->withServiceProviders([ GuzzleServiceProvider::class ]);
Basic operations
use Larafony\Framework\Validation\Attributes\Email; use Larafony\Framework\Validation\Attributes\IsValidated; use Larafony\Framework\Validation\Attributes\MinLength; use Larafony\Framework\Validation\FormRequest; use Larafony\Framework\Web\Config; use Larafony\Http\Guzzle\GuzzleHttpClient; use Larafony\Framework\Web\Application; use Larafony\Framework\Web\Controller; use Larafony\Framework\Routing\Advanced\Attributes\Route; use Larafony\Framework\Http\Factories\ResponseFactory; use Larafony\Http\Guzzle\GuzzleHttpClientFactory; use Psr\Http\Message\ResponseInterface; final class UserRequest extends FormRequest { //properties are automatically injected from the request #[IsValidated] #[MinLength(3)] public protected(set) string $firstName; #[IsValidated] #[MinLength(3)] public protected(set) string $lastName; #[IsValidated] #[Email] public protected(set) string $email; /** * @return array<string, string> */ public function toArray(): array { return [ 'firstName' => $this->firstName, 'lastName' => $this->lastName, 'email' => $this->email, ]; } } final class ApiController extends Controller { #[Route('/fetch-users')] public function fetchUsers(GuzzleHttpClient $client): \Psr\Http\Message\ResponseInterface { // Simple GET request (use guzzle client directly) $response = $client->getGuzzle()->get('https://api.example.com/users') |> $this->decodeResponse(...); return new ResponseFactory()->createJsonResponse($response); } #[Route('/update-users', methods: ['PUT', 'POST'])] public function updateUsers(GuzzleHttpClient $client, UserRequest $request): ResponseInterface { //use bridge facade $response = $client->post('https://api.example.com/users', [ 'json' => $request->toArray(), ]) |> $this->decodeResponse(...); return new ResponseFactory()->createJsonResponse($response); } #[Route('/authorized')] public function authorized(): ResponseInterface { $token = Config::get('api.token'); $client = GuzzleHttpClientFactory::withBearerToken($token); $response = $client->get('https://api.example.com/data') |> $this->decodeResponse(...); return new ResponseFactory()->createJsonResponse($response); } private function decodeResponse (ResponseInterface $response): array { return json_decode($response->getBody()->getContents(), associative: true, flags: JSON_THROW_ON_ERROR); } }
PSR-18 Compatibility
The Guzzle bridge implements PSR-18 ClientInterface, making it a drop-in replacement for Larafony's built-in HTTP client:
use Psr\Http\Client\ClientInterface; // Works with any PSR-18 compatible code function fetchData(ClientInterface $client): array { $response = $client->sendRequest($request); return json_decode($response->getBody()->getContents(), true); }
Advanced Features
// Concurrent requests $promises = [ 'users' => $client->getAsync('https://api.example.com/users'), 'posts' => $client->getAsync('https://api.example.com/posts'), ]; $results = \GuzzleHttp\Promise\Utils::unwrap($promises); // Retry middleware $client = $container->get(GuzzleHttpClient::class); $client->withMiddleware(new RetryMiddleware(maxRetries: 3)); // Upload files $response = $client->post('https://api.example.com/upload', [ 'multipart' => [ [ 'name' => 'file', 'contents' => fopen('/path/to/file.pdf', 'r'), ], ], ]);
Features
- PSR-18 compatible - Implements
ClientInterface - Full Guzzle API - Access all Guzzle features
- Async requests - Concurrent HTTP requests with promises
- Middleware system - Retry, logging, caching
- File uploads - Multipart form data support
- Streaming - Handle large responses efficiently
Why use this bridge?
While Larafony includes a built-in PSR-18 HTTP client, Guzzle offers:
- Async/concurrent request support
- Rich middleware ecosystem
- Automatic retries and error handling
- Cookie jar management
- Battle-tested codebase
Learn How It's Built - From Scratch
Interested in how Larafony is built step by step?
Check out my full PHP 8.5 course, where I explain everything from architecture to implementation - no magic, just clean code.
Get it now at masterphp.eu
License
MIT License. Larafony-http-guzzle is open-sourced software licensed under the MIT license.