fullpipe / php-json-rpc-client
JSON-RPC 2.0 客户端
v1.0.0
2021-01-17 13:19 UTC
Requires
- php: ^7.2 || ^8.0
- guzzlehttp/guzzle: ^6.4.1
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-17 21:10:54 UTC
README
安装
composer require fullpipe/php-json-rpc-client
使用
use Fullpipe\RpcClient\Client; use Fullpipe\RpcClient\Error\AppError; use Fullpipe\RpcClient\Error\MethodNotFound; use Fullpipe\RpcClient\Error\InvalidParams; ... $client = new Client('https://api.server/rpc', [ 'retries' => 0, 'delay' => 500, 'http' => ['timeout' => 1], ]); // Simple call $userData = $client->call('user.get', ['id' => 123]); // Simple call with single retry $userData = $client->retryOnce()->call('user.get', ['id' => 123]); // Call and catch application error try { $userData = $client->call('user.get', ['id' => 123]); } catch (AppError $e) { if ($e->getCode() !== 404) { throw $e; } $userData = $this->createNewUser(); } catch (MethodNotFound | InvalidParams $e) { $this->sentry->catchException($e); }
配置
默认设置
默认情况下禁用重试。CurlHandler 用作 guzzle 的处理器。
[ 'retries' => 0, 'retryCodes' => [500, 502, 503], 'delay' => 500, 'http' => ['timeout' => 1], // options for CurlHandler ]
自定义处理器
您可以使用自己的处理器。例如,用于测试。
use GuzzleHttp\Handler\MockHandler; ... $handler = new MockHandler([ new Response(200, [], \json_encode([ 'jsonrpc' => '2.0', 'result' => 'foo', 'id' => 1, ])), ]); $client = new Client('https://api.server/rpc', [ 'handler' => $handler ]);
重试代码
您可以将 retryCode 覆盖以在 RPC 错误上重试。
[
'retries' => 1,
'retryCodes' => [500, 502, 503, -32603],
]