graze/guzzle-jsonrpc

JSON-RPC 2.0 client for Guzzle

Installs: 41 503

Dependents: 2

Stars: 19

Watchers: 15

Forks: 5

Open Issues: 1

Language: PHP

2.1.0 2014-10-30 16:15 UTC

README

Master branch build status Published version PHP ~5.4 MIT Licensed

This library implements JSON-RPC 2.0 for the Guzzle HTTP client. We try to support all commonly used versions of Guzzle including:

It can be installed in whichever way you prefer, but we recommend Composer.

{
    "require": {
        "graze/guzzle-jsonrpc": "~2.1"
    }
}

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
$request->sendAll([
    $client->request(123, 'method', ['key'=>'value']),
    $client->request(456, 'method', ['key'=>'value']),
    $client->notification('method', ['key'=>'value'])
]);

Throw exception on RPC error

You can throw an exception if you receive an RPC error response by attaching a subscriber to either the client or the request. You probably won't want to do so with batch requests as the exception will only include the first bad response in your batch.

<?php
use Graze\GuzzleHttp\JsonRpc\Client;
use Graze\GuzzleHttp\JsonRpc\Exception\RequestException;
use Graze\GuzzleHttp\JsonRpc\Subscriber\ErrorSubscriber;

// Create the client
$client = Client::factory('http://localhost:8000');

// Create a request and attach the error subscriber
$request = $client->request(123, 'method', ['key'=>'value']);
$request->getEmitter()->attach(new ErrorSubscriber());

// 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.

$ composer install
$ make test

If you have Vagrant installed, you can build our dev environment to assist development. The repository will be mounted in /srv.

$ vagrant up
$ vagrant ssh
$ cd /srv

License

The content of this library is released under the MIT License by Nature Delivered Ltd.
You can find a copy of this license at http://www.opensource.org/licenses/mit or in LICENSE