efabrica / revolt-curl-client
Symfony's HttpClientInterface 的实现,结合了多curl 和 Revolt 的异步 EventLoop
0.1.0
2024-08-21 12:36 UTC
Requires
- php: ^8.1
- amphp/amp: ^3.0
- revolt/event-loop: ^1.0
- symfony/http-client: ^6.4|^7.0
Requires (Dev)
- phpunit/phpunit: ^10.5
README
这是从 symfony/http-client
的 CurlClient 分支出来的,它使用 Revolt 的异步 EventLoop 来等待响应而不是阻塞主线程。
创建此客户端的原因是,我们使用的原生 PHP 流(由 amphp/http-client
使用)速度较慢,我们的应用程序经常在 stream_select()
上挂起,但不会在 curl_multi_select()
上挂起。我们不知道这些原因,但如果您知道,请分享您的知识,我们将将其放入此 README 中。
它使用 Symfony 的 @internal 类,因此可能在较小的 Symfony 版本上崩溃。如果确实如此,我们将急忙修复它。
安装
composer require efabrica/revolt-curl-client
用法
use Efabrica\RevoltCurlClient\RevoltCurlClient; $client = new RevoltCurlClient(); $f1 = async(function () use ($client) { echo "Request 1\n"; $response = $client->request('GET', 'https://httpbin.org/get?1'); $response->getContent(); echo "Request 2\n"; $response2 = $client->request('GET', 'https://httpbin.org/get?2'); $response2->getContent(); }); $f2 = async(function () use ($client) { echo "Request 3\n"; $response = $client->request('GET', 'https://httpbin.org/get?3'); $response->getContent(); echo "Request 4\n"; $response2 = $client->request('GET', 'https://httpbin.org/get?4'); $response2->getContent(); }); await([$f1, $f2]); // Outputs: // Request 1 // Request 3 // Request 2 // Request 4