JsonRpcPhp

This PHP package implements JSON-RPC v2.0 protocol (specification).

Supports:

Basics

Package consists of 2 main classes: JsonRpcServer and JsonRpcClient. JsonRpcException.php file contains classes for each type of JSON-RPC error.

Server

JSON-RPC server can be initialized in the following way:

<?php
include '../JsonRpcException.php';
include '../JsonRpcServer.php';
include 'Service.php';

$service = new Service();
$server = new JsonRpcServer();

$server->addService($service);

$request = file_get_contents("php://input");
$result = $server->process($request);

if ($result !== null) {
    echo $result;
}
?>

Service.php is a class containing service methods which will be exposed by the server.

Multiple services can be plugged to server (with non-intersecting method names).

Example of service class:

<?php
class MathService
{
    public function divide($dividend, $divisor)
    {
        if ($divisor === 0) {
            throw new JsonRpcApplicationException('Division by zero', 100);
        }

        return $dividend/$divisor;
    }
}
?>

Error should be thrown from service method as an instance of JsonRpcApplicationException class

Client

JSON-RPC server can be initialized in the following way:

<?php
include '../JsonRpcClient.php';
$client = new JsonRpcClient('http://path-to-json-rpc-server');
?>

If there will be HTTP status code other than 200 when connecting to specified JSON-RPC server url, an exception will be thrown by JsonRpcClient.

Making JSON-RPC call

Using server context (invoking server method directly):

try {
    $params = new StdClass();
    $params->minuend = 42;
    $params->subtrahend = 23;

    $response = $client->subtract($params, 1);
    var_dump($response);
} catch (Exception $e) {
    ...
}
try {
    $params = array('minuend' => 42, 'subtrahend' => 23);
    $response = $client->subtract($params, 2);
    var_dump($response);
} catch (Exception $e) {
    ...
}

Using call method:

$response = $client->call('subtract', array(42, 23), 3);
var_dump($response);

$response = $client->call('subtract', array('subtrahend' => 23, 'minuend' => 42), 4);
var_dump($response);

Sending plain JSON encoded request using rawcall method:

$request = '{
    "jsonrpc":"2.0",
    "method":"subtract",
    "params":{"subtrahend":23,"minuend":42},
    "id":5
}';

$response = $client->rawcall($request);
var_dump($response);

Performing batch call:

$requests = array();

$requests[] = $client->prepare('subtract', 2, 1);
$requests[] = $client->prepare('subtract', array(23, 52), 2);
$requests[] = $client->prepare('subtract', array(45, 52), 3);
$requests[] = $client->prepare('subtract', array(7, 52), 4);
$requests[] = $client->prepare('foobar', array(7, 52), 5);

$response = $client->callBatch($requests);
var_dump($response);

More examples are provided with the package.

Examples contain tests, one for example requests and responses from specification, the second for additional testing and third for optional parameters in service methods.

All three should pass succefully.