PHP code example of psr-discovery / http-client-implementations

1. Go to this page and download the library: Download psr-discovery/http-client-implementations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

psr-discovery / http-client-implementations example snippets


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(...);

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();
}

use PsrDiscovery\Discover;

// $httpClient1 !== $httpClient2 (default)
$httpClient1 = Discover::httpClient();
$httpClient2 = Discover::httpClient();

// $httpClient1 === $httpClient2
$httpClient1 = Discover::httpClient(singleton: true);
$httpClient2 = Discover::httpClient(singleton: true);

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();

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();