mihasicehcek/php_json_rpc_2_server
Composer 安装命令:
composer require mihasicehcek/php_json_rpc_2_server
包简介
php realisation of JSON RPC 2 Specification
README 文档
README
Description
It is php implementation of the JSON-RPC 2.0 server
Working principle is very simple
- Create an instance of the class \PhpJsonRpc2\JsonRpcServer
- In method setRequest pass instance or array of \PhpJsonRpc2\Request
- Using method getResponse getting instance or array of instances \PhpJsonRpc2\Response
There is also a \PhpJsonRpc2\ICallStrategy interface that can configure \PhpJsonRpc2\JsonRpcServer for changing strategy of calling procedure. By default \PhpJsonRpc2\JsonRpcServer is configured by strategy \PhpJsonRpc2\ClassMethodCallStrategy, that divide request parameter "method"(for example Calculator.sum) by dot and create "Calculator" instance and call its method "sum".
Installation using composer
$ composer require composer require mihasicehcek/php_json_rpc_2_server
##Examples
###Simple Example
//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}
###Expamle with pre-conversion json string into object
You can convert json into instance or instances array before passing it into server object. That can be implemented using static method\PhpJsonRpc2\Request::requestFactory($json). But in this case you need to look after handling \PhpJsonRpc2\ParseErrorException, that can be throwed in case of invalid json parsing
$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}
##Notification
Notification if passed, the response is returned 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
##Batch
See Batch
$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}]
##Custom strategy call
You can define your own methods strategy call For example we want to provide "method" not as class.method, but invoke some simple functions
//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}
##Exceptions
All exceptions is subtype of base \PhpJsonRpc2\BaseJsonRpcException. Exceptions not throwing, but its instance returned in response.
$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}
If you want to throw an exception in procedures, you need to throw instance of \PhpJsonRpc2\InternalException or its subtype
mihasicehcek/php_json_rpc_2_server 适用场景与选型建议
mihasicehcek/php_json_rpc_2_server 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.69k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 02 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mihasicehcek/php_json_rpc_2_server 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mihasicehcek/php_json_rpc_2_server 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-07