2017 © Pedro Peláez
 

library jsonrpc

JsonRpc 2.0 PHP Specification

image

igaponov/jsonrpc

JsonRpc 2.0 PHP Specification

  • Wednesday, April 29, 2015
  • by igaponov
  • Repository
  • 1 Watchers
  • 2 Stars
  • 26 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

JSON-RPC 2.0 Specification

JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol., (*1)

Build Status Scrutinizer Code Quality Code Coverage, (*2)

Request object

A rpc call is represented by sending a Request object to a Server., (*3)

// client
$request = new \JsonRpc\Spec\Request('subtract', [42, 23], 1);

// server
$result = call_user_func_array($request->getMethod(), $request->getParams());

Notification

A Notification is a Request object without an "id" member., (*4)

$request1 = new \JsonRpc\Spec\Request('update', [1,2,3,4,5]);
$request2 = new \JsonRpc\Spec\Request('foobar');

Response object

When a rpc call is made, the Server MUST reply with a Response, except for in the case of Notifications., (*5)

$response = new \JsonRpc\Spec\Response($result, null, $request->getId());

Error object

When a rpc call encounters an error, the Response Object MUST contain the Error member with a value that is a \JsonRpc\Spec\Error, (*6)

$error = new \JsonRpc\Spec\Error(500, 'Internal error', $exception->getTraceAsString());
$response = new \JsonRpc\Spec\Response(null, $error, $request->getId());

The error codes from and including -32768 to -32000 are reserved for pre-defined errors., (*7)

use \JsonRpc\Spec\Exception\ParseErrorException;

try {
    // parse request
    throw new ParseErrorException();
} catch(ParseErrorException $e) {
    $error = new \JsonRpc\Spec\Error($e->getCode(), $e->getMessage(), $e->getTraceAsString());
    $response = new \JsonRpc\Spec\Response(null, $error, $request->getId());    
}

Batch

To send several Request objects at the same time, the Client MAY send an Array filled with Request objects., (*8)

foreach($batch as $response) {
    $result = $response->getResult();
}

BatchRequest

$requests = [
    new \JsonRpc\Spec\Request('update', [1,2,3,4,5]),
    new \JsonRpc\Spec\Request('foobar'),
    // ...
];
$batch = new \JsonRpc\Spec\BatchRequest($requests);

BatchResponse

$responses = [
    new \JsonRpc\Spec\Response(7, 1),
    new \JsonRpc\Spec\Response(null, $error, 2),
    // ...
];
$batch = new \JsonRpc\Spec\BatchResponse($responses);

ObjectManager

Object manager is a wrapper for dealing with requests/responses, (*9)

$manager = new \JsonRpc\ObjectManager($transport);
$id = $manager->addRequest('subtract', [42, 23]);
$manager->addNotification('foobar');
$manager->commit();
if (!$manager->hasError($id)) {
    $result = $manager->getResult($id); // 19
} else {
    throw new Exception($manager->getError($id), $manager->getErrorCode($id));
}

Transport

The object manager uses a transport object to communicate with a transport layer (http, rabbitmq, etc). The transport object must implements the \JsonRpc\TransportInterface., (*10)

class CurlTransport implements \JsonRpc\TransportInterface 
{
    public function send(UnitInterface $data) 
    {
        $ch = curl_init();        

        $data = json_encode($data);        

        curl_setopt($ch, CURLOPT_URL, 'http://localhost/rpc.php');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        curl_exec($ch);  
    }
}

Testing

bash $ phpunit, (*11)

License

The MIT License (MIT). Please see License File for more information., (*12)

The Versions

29/04 2015

dev-master

9999999-dev https://github.com/igaponov/jsonrpc

JsonRpc 2.0 PHP Specification

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Gaponov Igor

specification json-rpc

29/04 2015

v1.0

1.0.0.0 https://github.com/igaponov/jsonrpc

JsonRpc 2.0 PHP Specification

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Gaponov Igor

specification json-rpc