Skip to main content

Switch by Preset

use Cognesy\Http\HttpClient;

$client = HttpClient::using('guzzle');
$client = HttpClient::using('symfony');
// @doctest id="e934"
Equivalent builder form:
use Cognesy\Http\Creation\HttpClientBuilder;

$client = (new HttpClientBuilder())
    ->withPreset('guzzle')
    ->create();
// @doctest id="34c5"

Inject an Explicit Driver

use Cognesy\Http\Creation\HttpClientBuilder;
use Cognesy\Http\Drivers\Mock\MockHttpDriver;

$client = (new HttpClientBuilder())
    ->withDriver(new MockHttpDriver())
    ->create();
// @doctest id="c376"

Use an Existing Vendor Client Instance

use Cognesy\Http\Creation\HttpClientBuilder;
use GuzzleHttp\Client;

$client = (new HttpClientBuilder())
    ->withClientInstance('guzzle', new Client(['timeout' => 10]))
    ->create();
// @doctest id="c3a3"
withClientInstance() sets the driver name and passes the instance to that driver.

Override Pool Handling for Custom Drivers

use Cognesy\Http\Creation\HttpClientBuilder;

$client = (new HttpClientBuilder())
    ->withDriver($customDriver)
    ->withPoolHandler($customPoolHandler)
    ->create();
// @doctest id="7d86"
Use this when your custom driver cannot use built-in pooling adapters.

See Also