Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fullpipe/php-json-rpc-client
JSON-RPC 2.0 PHP client.
https://github.com/fullpipe/php-json-rpc-client
json-rpc json-rpc-client
Last synced: about 2 months ago
JSON representation
JSON-RPC 2.0 PHP client.
- Host: GitHub
- URL: https://github.com/fullpipe/php-json-rpc-client
- Owner: fullpipe
- License: mit
- Created: 2020-02-08T16:56:39.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-17T13:20:08.000Z (almost 4 years ago)
- Last Synced: 2024-05-10T14:02:26.893Z (8 months ago)
- Topics: json-rpc, json-rpc-client
- Language: PHP
- Homepage:
- Size: 52.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON-RPC 2.0 PHP client
## Install
```
composer require fullpipe/php-json-rpc-client
```## Usage
```php
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);
}
```## Configuration
### Default
By default retries disabled. And CurlHandler used as handler for guzzle.
```php
[
'retries' => 0,
'retryCodes' => [500, 502, 503],
'delay' => 500,
'http' => ['timeout' => 1], // options for CurlHandler
]
```### Custom handler
You could use you own handler. For tests for example.
```php
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
]);
```### Retry codes
You could overwrite retryCode to retry on RPC errors
```
[
'retries' => 1,
'retryCodes' => [500, 502, 503, -32603],
]
```