sunrise/http-client-curl

基于PSR-18的简单PHP 7.1+ cURL HTTP客户端


README

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

安装

composer require sunrise/http-client-curl

快速入门

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', 'https://php.ac.cn/');
$response = $client->sendRequest($request);

echo $response->getStatusCode(), PHP_EOL;

cURL选项

https://php.ac.cn/manual/ru/curl.constants.php

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

多个请求的并行执行

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

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

foreach ($responses as $i => $response) {
    // note that you can get the response's request by its index...
    echo sprintf('%s => %d', $requests[$i]->getUri(), $response->getStatusCode()), PHP_EOL;
}

测试运行

composer test

有用链接