sunrise/http-client-curl

Sunrise // HTTP cURL client for PHP 7.1+ (incl. PHP 8) based on PSR-18

v1.4.1 2021-10-13 03:53 UTC

README

Build Status Code Coverage Scrutinizer Code Quality Total Downloads Latest Stable Version License

Installation

composer require sunrise/http-client-curl

How to use?

Sunrise

composer require sunrise/http-factory
use Sunrise\Http\Client\Curl\Client;
use Sunrise\Http\Factory\RequestFactory;
use Sunrise\Http\Factory\ResponseFactory;

$client = new Client(new ResponseFactory());
$request = (new RequestFactory)->createRequest('GET', 'http://php.net/');
$response = $client->sendRequest($request);

// just use PSR-7 response...

Zend Diactoros

composer require zendframework/zend-diactoros
use Sunrise\Http\Client\Curl\Client;
use Zend\Diactoros\RequestFactory;
use Zend\Diactoros\ResponseFactory;

$client = new Client(new ResponseFactory());
$request = (new RequestFactory)->createRequest('GET', 'http://php.net/');
$response = $client->sendRequest($request);

// just use PSR-7 response...

cURL options

$client = new Client(new ResponseFactory(), [
    \CURLOPT_AUTOREFERER => true,
    \CURLOPT_FOLLOWLOCATION => true,
    \CURLOPT_MAXREDIRS => 10,
]);

Asynchronous execution of multiple requests

$requests = [
    (new RequestFactory)->createRequest('GET', 'http://php.net/'),
    (new RequestFactory)->createRequest('GET', 'http://php.net/'),
];

$client = new Client(new ResponseFactory());
$responses = $client->sendRequests(...$request);

foreach ($responses as $response) {
    // just use PSR-7 response...
}

Test run

composer test

Useful links