evently / guzzle-jsonrpc
支持Guzzle 7的兼容性的Guzzle JSON-RPC 2.0客户端,基于graze/guzzle-jsonrpc分支
3.3.1
2022-05-26 13:13 UTC
Requires
- php: >=7.0
- guzzlehttp/guzzle: ^7.4.3
- guzzlehttp/promises: ^1.5
- psr/http-message: ^1.0
Requires (Dev)
- adlawson/timezone: ~1.0
- graze/standards: ^1.0
- mockery/mockery: ~0.9
- phpunit/phpunit: ^4|^5
- squizlabs/php_codesniffer: ^2.9
README
此库实现了JSON-RPC 2.0,为Guzzle HTTP客户端提供支持。我们尝试支持Guzzle的所有常用版本,包括
- GuzzleHTTP 6在
master
分支,^3.0
版本 - GuzzleHTTP 5在
guzzle-5
分支,^2.1
版本 - GuzzleHTTP 4在
guzzle-4
分支,2.0.*
版本 - Guzzle 3在
guzzle-3
分支,^1.0
版本
您可以选择任何您喜欢的安装方式,但我们推荐使用Composer。
{ "require": { "graze/guzzle-jsonrpc": "~3.0" } }
文档
<?php use Graze\GuzzleHttp\JsonRpc\Client; // Create the client $client = Client::factory('https://:8000'); // Send a notification $client->send($client->notification('method', ['key'=>'value'])); // Send a request that expects a response $client->send($client->request(123, 'method', ['key'=>'value'])); // Send a batch of requests $client->sendAll([ $client->request(123, 'method', ['key'=>'value']), $client->request(456, 'method', ['key'=>'value']), $client->notification('method', ['key'=>'value']) ]);
异步请求
通过使用Guzzle Promises库支持异步请求;它实现了Promises/A+。
<?php use Graze\GuzzleHttp\JsonRpc\Client; // Create the client $client = Client::factory('https://:8000'); // Send an async notification $promise = $client->sendAsync($client->notification('method', ['key'=>'value'])); $promise->then(function () { // Do something }); // Send an async request that expects a response $promise = $client->sendAsync($client->request(123, 'method', ['key'=>'value'])); $promise->then(function ($response) { // Do something with the response }); // Send a batch of requests $client->sendAllAsync([ $client->request(123, 'method', ['key'=>'value']), $client->request(456, 'method', ['key'=>'value']), $client->notification('method', ['key'=>'value']) ])->then(function ($responses) { // Do something with the list of responses });
在RPC错误时抛出异常
如果您收到RPC错误响应,可以在客户端构造函数中添加选项[rpc_error => true]
来抛出异常。
<?php use Graze\GuzzleHttp\JsonRpc\Client; use Graze\GuzzleHttp\JsonRpc\Exception\RequestException; // Create the client with the `rpc_error` $client = Client::factory('https://:8000', ['rpc_error'=>true]); // Create a request $request = $client->request(123, 'method', ['key'=>'value']); // Send the request try { $client->send($request); } catch (RequestException $e) { die($e->getResponse()->getRpcErrorMessage()); }
贡献
我们接受通过Pull Request提交的源代码贡献,但在合并之前必须包含单元测试。
~ $ make deps ~ $ make lint test
许可证
此库的内容由Nature Delivered Ltd在MIT许可证下发布。