phasync / http-client
异步PSR-18 HTTP客户端实现。
1.0.0
2024-06-25 09:43 UTC
Requires
- charm/options: ^1.1.5
- phasync/phasync: ^1.0.7
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- pestphp/pest: ^2.34
README
phasync HTTP客户端是一个强大的基于纤程的PHP HTTP客户端,利用PHP纤程的并发特性来高效地管理非阻塞请求。此客户端符合PSR-18规范,并允许通过cURL选项进行广泛的配置,以根据您的需求定制请求处理。
功能
- 并发HTTP请求:利用PHP纤程执行非阻塞HTTP请求。
- 完全PSR-18兼容:完全符合PSR-18 HTTP客户端接口。
- 广泛配置:使用一系列cURL选项自定义HTTP请求的各个方面。
安装
使用Composer在项目中安装Phasync HTTP客户端
composer require phasync/http-client
用法
基本用法
以下是一个并发GET请求的简单示例
use phasync\HttpClient\HttpClient; $client = new HttpClient(); // Concurrent requests: $response1 = $client->get('https://httpbin.org/get'); $response2 = $client->get('https://www.reddit.com/"); // All requests are performed in parallel using `curl_multi` under the hood. echo $response1->getBody(); echo $resposne2->getBody();
POST请求
发送包含数据的POST请求
$response = $client->post('https://httpbin.org/post', [ 'foo' => 'bar' ]); echo $response->getBody();
PSR-18客户端用法
客户端支持PSR-18客户端规范
$psr7Response = $client->sendRequest($psr7Request);
处理重定向
自动处理重定向
$client = new HttpClient([ 'followLocation' => true ]); $response = $client->get('https://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com');
自定义cURL选项
通过设置cURL选项来自定义客户端行为
$client = new HttpClient([ 'timeoutMs' => 1000, 'userAgent' => 'PhasyncClient/1.0' ]);
配置选项
HttpClientOptions
类提供了一种配置处理请求的各种选项的方法
userAgent
:设置'User-Agent'头。timeoutMs
:允许cURL函数执行的最大毫秒数。followLocation
:跟随重定向。sslVerifyPeer
:验证对等方的SSL证书。- 更多详细信息请参阅类定义。
请参阅HttpClientOptions
类以获取所有可配置选项的完整列表。
贡献
欢迎贡献!请随时提交拉取请求或创建有关错误和功能请求的问题。
许可
本项目是开源软件,受MIT许可协议许可。