timmachine/php-json-rpc

此包的最新版本(dev-dev)没有可用的许可证信息。

dev-dev 2014-11-26 06:27 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:54:27 UTC


README

Build Status

## 路由器:路由器允许您将字符串映射到类内部的方法。@ 符号用于分隔类和方法名。它本身并不做什么,但对于监听器是必需的。

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

监听器

监听器是操作的大脑。这个家伙将接收您的 Json 请求,对其进行转换并执行您想要调用的方法,然后将数据以正确格式化的 JsonRPC 2.0 格式返回给您。

    $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();
    }

新的 JsonRPC 版本?

确保我们有些前瞻性。

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

定制需求?

也许您的应用程序有一些定制需求?

$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);

可选参数

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}