drewlabs / psr18
基于PHP cURL客户端库的PSR18兼容的HTTP客户端
v0.4.2
2024-08-16 01:12 UTC
Requires
- drewlabs/curl-client: ^0.4.0
- drewlabs/psr7: ^0.1.5|^0.2.0
- psr/http-client: ^1.0
Requires (Dev)
- donatj/mock-webserver: ^2.6
- phpunit/phpunit: >=7.0
README
该项目提供了一个基于cURL客户端对象的PSR18客户端实现。
安装
推荐使用PHP包管理器composer
安装库,运行以下命令
composer require drewlabs/psr18
用法
PSR18客户端
该软件包包含一个使用PHP cURL库的PSR18兼容客户端。要创建客户端实例
use Drewlabs\Psr18\Client; // Creates an instance of the cURL client $client = Client::new(/* Parameters */); // Passing constructor parameters $client = Client::new('http:://127.0.0.1:5000'); // Passing customize client options $client = Client::new([ /* Custom client options */ ]);
客户端选项
客户端选项,为开发者提供了一种覆盖传递给Client::sendRequest()
方法参数的方式。该软件包提供了一个PHP类来构建客户端选项,作为使用PHP字典类型(即PHP数组)的替代方案。
- 使用工厂函数创建客户端选项
use Drewlabs\Curl\ClientOptions; // $clientOptions = ClientOptions::create([ 'verify' => false, 'sink' => null, 'force_resolve_ip' => false, 'proxy' => ['http://proxy.app-ip.com'], 'cert' => null, 'ssl_key' => ['/home/webhost/.ssh/pub.key'], 'progress' => new class { // Declare the function to handle the progress event public function __invoke() { // Handle the progress event } }, 'base_url' => 'http://127.0.0.1:3000', 'connect_timeout' => 1000, 'request' => [ 'headers' => [ 'Content-Type' => 'application/json' ], 'timeout' => 10, 'auth' => ['MyUser', 'MyPassword', 'digest'], 'query' => [ 'post_id' => 2, 'comments_count' => 1 ], 'encoding' => 'gzip,deflate' ], 'cookies' => [ 'clientid' => 'myClientID', 'clientsecret' => 'MySuperSecret' ], ]);
- 使用流畅API
除了使用工厂函数之外,我们还可以使用流畅API来创建客户端选项。流畅API通过提供定义选项值的方法来尝试减少开发者的打字错误。
use Drewlabs\Curl\ClientOptions; use Drewlabs\Curl\RequestOptions; $clientOptions = new ClientOptions; $clientOptions->setBaseURL(/* base url*/) ->setRequest(RequestOptions::create([])) ->setConnectTimeout(150) ->setVerify(false) // Psr Stream to write response output to ->setSink() ->setForceResolveIp(true) ->setProxy($proxy_ip, [$proxy_port, $user, $password]) // port, user & password are optional depending on the proxy configuration ->setCert('/path/to/ssl/certificate'); ->setSslKey('/path/to/ssl/key') ->setProgress(function($curl, ...$progress) { // Handle cURL progress event }) ->setCookies([]); // List of request cookies
注意 请求选项和客户端选项流畅API的API可以在API参考文档中找到。
发送PSR18请求
发送请求就像使用任何PSR18兼容库一样简单
use Drewlabs\Psr18\Client; use Drewlabs\Psr7\Request; // Creates an instance of the cURL client $client = Client::new([ // Parameters to client options ... 'base_url' => 'http://127.0.0.1:3000', 'connect_timeout' => 1000, 'request' => [ 'headers' => [ 'Content-Type' => 'application/json' ], 'timeout' => 10, 'auth' => ['MyUser', 'MyPassword', 'digest'], 'query' => [ 'post_id' => 2, 'comments_count' => 1 ], 'encoding' => 'gzip,deflate' ], ]); $response = $client->sendRequest(new Request()); // \Psr7\Http\ResponseInterface
要发送JSON请求,开发者应在向服务器发送请求之前调用Client::json()
方法
use Drewlabs\Psr18\Client; // Creates an instance of the cURL client $client = new Client(/* Parameters */); // Sends a request with application/json as Content-Type $client->json()->sendRequest(/* PSR7 compatible Request */);
或者,要发送multipart/form-data
请求,开发者应在向服务器发送请求之前调用Client::multipart()
方法
use Drewlabs\Psr18\Client; // Creates an instance of the cURL client $client = new Client( 'verify' => false, 'request' => [ 'headers' => ['Accept' => 'application/json'], 'body' => [ /*...*/ ], ], ); // Sends a request with application/json as Content-Type $client->multipart()->sendRequest(new Request());