Lightweight library that discovers available PSR-18 HTTP Client implementations by searching for a list of well-known classes that implement the relevant interface, and returns an instance of the first one that is found.
This package is part of the PSR Discovery utility suite, which also supports PSR-17 HTTP Factories, PSR-14 Event Dispatchers, PSR-11 Containers, PSR-6 Caches and PSR-3 Logs.
This is largely intended for inclusion in libraries like SDKs that wish to support PSR-18 HTTP Clients without requiring hard dependencies on specific implementations or demanding extra configuration by users.
- Requirements
- Implementations
- Installation
- Usage
- Handling Failures
- Exceptions
- Singletons
- Mocking Priority
- Preferring an Implementation
- Using a Specific Implementation
- PHP 8.2+
- Composer 2.0+
Successful discovery requires the presence of a compatible implementation in the host application. This library does not install any implementations for you.
The following psr/http-client-implementation
implementations are discovered and instantiated automatically:
- guzzlehttp/guzzle ^7.0 | ^8.0
- joomla/http ^2.0 | ^3.0
- kriswallsmith/buzz ^1.0
- php-http/curl-client ^2.1
- php-http/guzzle5-adapter ^2.0
- php-http/guzzle6-adapter ^2.0
- php-http/guzzle7-adapter ^0.1 | ^1.0
- php-http/socket-client ^2.0
- symfony/http-client ^4.3 | ^5.0 | ^6.0 | ^7.0 | ^8.0
- voku/httpful ^2.2 | ^3.0
- nimbly/shuttle ^1.0
The following mock implementations are also available:
If a particular implementation is missing that you'd like to see, please open a pull request adding support.
composer require psr-discovery/http-client-implementations
use PsrDiscovery\Discover;
// Return an instance of the first discovered PSR-18 HTTP Client implementation.
$httpClient = Discover::httpClient();
// Send a request using the discovered HTTP Client.
$httpClient->sendRequest(...);
If the library is unable to discover a suitable PSR-18 implementation, the Discover::httpClient()
discovery method will simply return null
. This allows you to handle the failure gracefully, for example by falling back to a default implementation.
Example:
use PsrDiscovery\Discover;
$httpClient = Discover::httpClient();
if ($httpClient === null) {
// No suitable HTTP Client implementation was discovered.
// Fall back to a default implementation.
$httpClient = new DefaultHttpClient();
}
By default, the Discover::httpClient()
method will always return a new instance of the discovered implementation. If you wish to use a singleton instance instead, simply pass true
to the $singleton
parameter of the discovery method.
Example:
use PsrDiscovery\Discover;
// $httpClient1 !== $httpClient2 (default)
$httpClient1 = Discover::httpClient();
$httpClient2 = Discover::httpClient();
// $httpClient1 === $httpClient2
$httpClient1 = Discover::httpClient(singleton: true);
$httpClient2 = Discover::httpClient(singleton: true);
This library will give priority to searching for a known, available mocking library before searching for a real implementation. This is to allow for easier testing of code that uses this library.
The expectation is that these mocking libraries will always be installed as development dependencies, and therefore if they are available, they are intended to be used.
If you wish to prefer a specific implementation over others, you can prefer()
it by package name:
use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr18\Clients;
// Prefer the a specific implementation of PSR-18 over others.
Clients::prefer('guzzlehttp/guzzle');
// Return an instance of GuzzleHttp\Client,
// or the next available from the list of candidates,
// Returns null if none are discovered.
$client = Discover::httpClient();
This will cause the httpClient()
method to return the preferred implementation if it is available, otherwise, it will fall back to the default behavior.
Note that assigning a preferred implementation will give it priority over the default preference of mocking libraries.
If you wish to force a specific implementation and ignore the rest of the discovery candidates, you can use()
its package name:
use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr18\Clients;
// Only discover a specific implementation of PSR-18.
Clients::use('guzzlehttp/guzzle');
// Return an instance of GuzzleHttp\Client,
// or null if it is not available.
$client = Discover::httpClient();
This will cause the httpClient()
method to return the preferred implementation if it is available, otherwise, it will return null
.
This library is not produced or endorsed by, or otherwise affiliated with, the PHP-FIG.