Guzzle JSON-RPC
This library implements JSON-RPC 2.0 for the Guzzle HTTP client. We try to support all commonly used versions of Guzzle including:
- GuzzleHTTP 6 on
masterbranch,^3.0releases - GuzzleHTTP 5 on
guzzle-5branch,^2.1releases - GuzzleHTTP 4 on
guzzle-4branch,2.0.*releases - Guzzle 3 on
guzzle-3branch,^1.0releases
It can be installed in whichever way you prefer, but we recommend Composer.
{
"require": {
"graze/guzzle-jsonrpc": "~3.0"
}
}Documentation
<?php
use Graze\GuzzleHttp\JsonRpc\Client;
// Create the client
$client = Client::factory('http://localhost: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'])
]);Async requests
Asynchronous requests are supported by making use of the Guzzle Promises library; an implementation of Promises/A+.
<?php
use Graze\GuzzleHttp\JsonRpc\Client;
// Create the client
$client = Client::factory('http://localhost: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
});Throw exception on RPC error
You can throw an exception if you receive an RPC error response by adding the
option [rpc_error => true] in the client constructor.
<?php
use Graze\GuzzleHttp\JsonRpc\Client;
use Graze\GuzzleHttp\JsonRpc\Exception\RequestException;
// Create the client with the `rpc_error`
$client = Client::factory('http://localhost: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());
}Contributing
We accept contributions to the source via Pull Request, but passing unit tests must be included before it will be considered for merge.
~ $ make deps
~ $ make lint testLicense
The content of this library is released under the MIT License by Nature Delivered Ltd.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
