moaction/jsonrpc-server

Jsonrpc server php implementation

v1.1 2013-12-20 08:23 UTC

This package is not auto-updated.

Last update: 2024-05-20 23:34:08 UTC


README

Server implementation for JsonRPC 2.0 protocol

http://www.jsonrpc.org/specification

Usage

Basic usage

$server = new \Moaction\Jsonrpc\Server\BasicServer();
$server->addMethod('getUser', function($id) {
  return array(
    'id'   => $id,
    'name' => 'UserName'
  );
});

echo $server->run(file_get_contents('php://input'));

Error reporting

Every exception in method call will be converted into error object in response. You can specify code and message in exception.

$server->addMethod('errorTest', function() {
  throw new \Exception('Strange server error', 42);
});

Server response will be:

{"jsonrpc": "2.0", "error": {"code": 42, "message": "Strange server error"}, "id": null}

If you do not provide code, default "Server Error" code -32000 will be used. As well as error message.