This library provides the server side of a JSON-RPC system for PHP. It is designed to be simple to use and framework agnostic. You should be able to use it with any PHP web framework, or even without any framework at all.
Contents
There are three steps required to use this library:
-
Create the RPC system.
use RPC\JsonRpcSystem; use RPC\Codec\JsonRequestDeserializer; use RPC\Codec\JsonResponseSerializer; $rpcSystem = new JsonRpcSystem( new JsonRequestDeserializer(), new JsonResponseSerializer() );
-
Register one or more remote procedures.
$rpcSystem->registerProcedure('hello', function($name) { return "Hello, $name!"; });
-
Call the procedures.
$rpcRequest = [ 'jsonrpc' => '2.0', 'method' => 'hi', ]; $response = $this->rpcSystem->run($rpcRequest);
This library only supports a subset of the JSON-RPC 2.0 Specification. The list below shows the features implemented so far.
- Calling a single procedure
- Calling multiple procedures in batch
- Named parameter lists
- Notifications
This library correctly handles every error case defined in the spec. It returns the correct error Json for each of the following:
- Parse error
- Invalid Request
- Method not found
- Invalid params
- Internal error
It's rare for software to be "done". This library may get there some day. But until then, here is the currently planned work.
- Make this library a proper PHP library that can be installed with Composer.