mihasicehcek / php_json_rpc_2_server
PHP 实现的 JSON RPC 2 规范
Requires
Requires (Dev)
- phpunit/phpunit: 5.5.*
README
描述
它是 JSON-RPC 2.0 服务器的 PHP 实现
工作原理非常简单
- 创建 \PhpJsonRpc2\JsonRpcServer 类的实例
- 在方法 setRequest 中传入实例或 \PhpJsonRpc2\Request 数组
- 使用 getResponse 方法获取实例或 \PhpJsonRpc2\Response 数组实例
还有一个 \PhpJsonRpc2\ICallStrategy 接口,可以配置 \PhpJsonRpc2\JsonRpcServer 以更改调用过程策略。默认情况下,\PhpJsonRpc2\JsonRpcServer 由 \PhpJsonRpc2\ClassMethodCallStrategy 策略配置,该策略通过点将请求参数 "method"(例如 Calculator.sum)分割成类和方法,并创建 "Calculator" 实例,调用其 "sum" 方法。
使用 composer 安装
$ composer require composer require mihasicehcek/php_json_rpc_2_server
##示例
###简单示例
//simulating a request $json = json_encode([ "jsonrpc" => "2.0", "method" => "Calculator.sum", "params" => [ "a" => 10, "b" => 15], "id" => 1 ]); //Creating instance of \PhpJsonRpc2\JsonRpcServer $jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer(); //Passing json string $jsonRpcServer->setRequestAsJson($json); //geting Response $response = $jsonRpcServer->getResponse(); echo json_encode($response); //{ jsonrpc : "2.0", result : 2, id : 1}
###将预转换的 JSON 字符串转换为对象的示例
您可以在将 JSON 传递到服务器对象之前将其转换为实例或实例数组。这可以通过静态方法 \PhpJsonRpc2\Request::requestFactory($json) 实现。但在此情况下,您需要处理 \PhpJsonRpc2\ParseErrorException,该异常可能在无效 JSON 解析的情况下抛出。
$json = json_encode([ "jsonrpc" => "2.0", "method" => "Calculator.sum", "params" => [10, 15], "id" => 1 ]); //Create request from json $request = Request::requestFactory($json); $jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer(); //pass json into setRequest method $jsonRpcServer->setRequest($request); $response = $jsonRpcServer->getResponse(); echo json_encode($response); //{ jsonrpc : "2.0", result : 2, id : 1}
##通知
通知 如果传递,则响应返回 null。
$json = json_encode([ "jsonrpc" => "2.0", "method" => "Calculator.saveSum", "params" => [15], "id" => null ]); $jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer(); $jsonRpcServer->setRequestAsJson($json); $response = $jsonRpcServer->getResponse(); //null
##批量处理
请参阅 批量处理
$json = json_encode([ [ "jsonrpc" => "2.0", "method" => "Calculator.sum", "params" => [15, 15], "id" => 1 ], [ "jsonrpc" => "2.0", "method" => "Calculator.sum", "params" => [10, 50], "id" => 2 ] ]); $jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer(); $jsonRpcServer->setRequestAsJson($json); $response = $jsonRpcServer->getResponse(); //null json_encode($response) //[{ jsonrpc : "2.0", result : 15, id : 1},{ jsonrpc : "2.0", result : 60, id : 2}]
##自定义策略调用
您可以定义自己的方法调用策略。例如,我们希望将 "method" 不是作为 class.method,而是调用一些简单的函数。
//Create our implementation if \PhpJsonRpc2\ICallStrategy interface(For simplicity, we imagine that we pass only the positional parameters) class SimpleMethodCallStrategy implements ICallStrategy { /** *@param $method string method name(define in "method" parameter of json object) *@param $params array It can be as indexing and associative array */ public function call($method, $params) { $function = new \ReflectionFunction($method); return $function->invokeArgs($params); } } $request = [ "jsonrpc" => "2.0", "method" => "sum", "params" => [10, 15], "id" => 1 ]; $json = json_encode($request); $jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer(); //configure instance of JsonRpcServer by insatnce of SimpleMethodCallStrategy $jsonRpcServer->setCallStrategy(new SimpleMethodCallStrategy()); $jsonRpcServer->setRequestAsJson($json); $response = $jsonRpcServer->getResponse(); echo json_encode($response); //{ jsonrpc : "2.0", result : 2, id : 1}
##异常
所有异常都是基类 \PhpJsonRpc2\BaseJsonRpcException 的子类型。异常不会抛出,但会以其实例的形式返回在响应中。
$json = json_encode('{"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz]'); $jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer(); $jsonRpcServer->setRequestAsJson($json); $response = $jsonRpcServer->getResponse(); echo json_encode($response); //{ jsonrpc : "2.0", error : { code : -32700, message : "Parse error"}, id : null}
如果您想在程序中抛出异常,则需要抛出 \PhpJsonRpc2\InternalException 或其子类的实例。