solophp / http-client
Immutable PSR-18 HTTP client built on Guzzle: fluent API, safe retries, origin-bound credentials and redacted logs.
Requires
- php: ^8.3
- guzzlehttp/guzzle: ^8.3
- guzzlehttp/promises: ^3.1
- guzzlehttp/psr7: ^3.2
- psr/http-client: ^1.0
- psr/log: ^3.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.5
- squizlabs/php_codesniffer: ^3.13
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Immutable PSR-18 HTTP client for PHP, built on Guzzle. Every configuration call returns a new client, so one instance can be shared safely; credentials go only to the origin of the base URI; retries are opt-in and never repeat a POST the server may have processed; files are attached only through explicit File objects; and query values, URI credentials and registered secrets are masked in logs and exception messages.
Installation
composer require solophp/http-client
Requires PHP 8.1+ and Guzzle 7.15.2+.
Example
use Solo\HttpClient\HttpClient; $api = HttpClient::create('https://api.example.com/v1/') ->timeout(10) ->withToken($token) // sent to https://api.example.com only ->retry(3, 500); // off unless you ask for it $response = $api->get('users', ['query' => ['page' => 2]]); if ($response->successful()) { $users = $response->json('data'); // null when the body is not valid JSON } $api->post('users', ['json' => ['name' => 'Ada', 'email' => 'ada@example.com']]);
HTTP error statuses come back as responses; a failed transfer throws a PSR-18 exception. Every request method has an async variant, and sendRequest() serves code that depends on Psr\Http\Client\ClientInterface.
Documentation
- Getting Started
- Sharing a Client
- Authentication
- Retries
- File Uploads
- Logging and Redaction
- Async Requests
- Error Handling
- Testing with a Mock Handler
- API Reference
- Behavior in Detail
- Upgrading from 3.x
- Changelog
License
MIT. See LICENSE.