sunrise/http-client-curl

Simple HTTP cURL client for PHP 7.1+ based on PSR-18

v1.4.6 2022-04-19 19:08 UTC

README

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

Installation

composer require sunrise/http-client-curl

QuickStart

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

echo $response->getStatusCode(), PHP_EOL;

cURL options

https://www.php.net/manual/ru/curl.constants.php

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

Parallel 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 $i => $response) {
    // note that you can get the response's request...
    echo sprintf('%d <= %s', $response->getStatusCode(), $requests[$i]->getUri()), PHP_EOL;
}

Test run

composer test

Useful links