2017 © Pedro Peláez
 

library php-json-rpc

image

timmachine/php-json-rpc

  • Wednesday, November 26, 2014
  • by tsmith86
  • Repository
  • 1 Watchers
  • 1 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

Router

the router allows you to map a string to a method inside of the class. The @ symbol is used to separate the class from the method name. It really doesn't do much on its own, but is a requirement for the Listener, (*2)

$router = new \Timmachine\PhpJsonRpc\Router();
$router->add("math.subtract", 'BaseController@subtract');
$router->add("math.add", 'BaseController@add');

Listener

The listener is the brains of the operation. This bad boy will take your Json request twist it around and execute the method you want to call and then return the data to you in a properly formatted JsonRPC 2.0 format, (*3)


$json = '{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}'; $listener = new \Timmachine\PhpJsonRpc\Listener($routes); try { //validate our json $listener->validateJson($json); // process the method request $listener->processRequest(); // return our json response return $listener->getResponse(); } catch (\Timmachine\PhpJsonRpc\RpcExceptions $e) { // even if there is an error you send a response back to your client that is properly formatted return $listener->getResponse(); }

New JsonRPC versions?

lets make sure that we are forward thinking a little, (*4)

    $listener = new \Timmachine\PhpJsonRpc\Listener($routes,'2.1');

Custom Requirements ?

Maybe your application has some custom requirements ?, (*5)


$customRequirements = [ [ 'key' => 'myCustomKey', 'value' => null //no defined required value, 'errorMessage' => 'my custom error message', 'errorCode' => -32600 // Invalid params ], [ 'key' => 'myCustomKey2', 'value' => '1237485' //defined required value, 'errorMessage' => 'myCustomKey2 is not set correctly or missing', 'errorCode' => -32600 // Invalid params ] ]; $mySpec = '3.0' $listener = new \Timmachine\PhpJsonRpc\Listener($routes,$mySpec, $customRequirements);

Optional Params


class foo{ public function bar($a = 2,$b = 3, $c = 4) { return $a + $b + $c; } } // example json to make the request // '{"jsonrpc": "2.0", "method": "foo.bar", "params":[], "id": 1}'; //example response //{"jsonrpc": "2.0", "result": 9, "id": 1}

The Versions

26/11 2014

dev-dev

dev-dev

  Sources   Download

The Requires

 

by Tim Smith