gmicommunity / guzzle-jsonrpc
Guzzle 的 JSON-RPC 2.0 客户端
此软件包的官方仓库似乎已不存在,因此该软件包已被冻结。
3.2.1
2016-02-16 15:18 UTC
Requires
- php: >=5.5
- guzzlehttp/guzzle: ^6.0
- guzzlehttp/promises: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- adlawson/timezone: ~1.0
- fabpot/php-cs-fixer: ~0.5
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.3
README
这个库实现了 Guzzle HTTP 客户端的 JSON-RPC 2.0。我们尝试支持包括以下所有常用版本的 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 许可证 发布。