avto-dev / json-rpc-laravel

v2.6.0 2024-05-31 11:23 UTC

This package is auto-updated.

Last update: 2024-08-31 00:27:59 UTC


README

logo

JSON-RPC 2.0

Version PHP Version Build Status Coverage Downloads count License

JSON-RPC 2.0 是一种使用 JSON 编码的远程过程调用协议。这是一个非常简单的协议,只定义了少数几种数据类型和命令。JSON-RPC 允许发送通知(发送到服务器且不需要响应的数据)以及允许向服务器发送多个调用,这些调用可能以不同的顺序得到响应。

安装

使用以下命令通过 composer 安装此包

$ composer require avto-dev/json-rpc-laravel "^2.0"

需要安装 composer如何安装 composer)。

您需要修复包的主版本。

使用示例

创建路由

./routes/web.php 中使用 RpcRouter 面板注册您方法的操作

<?php

use AvtoDev\JsonRPC\RpcRouter;

RpcRouter::on('please_sum_array_values', 'YourNamespace\\SomeController@sum');
RpcRouter::on('show_full_request', 'YourNamespace\\SomeController@showInfo');

此包已经包含一个 简单控制器 实现,您可以扩展它或仅将其作为示例。

RpcController 添加新路由

<?php

use Illuminate\Support\Facades\Route;

Route::post('/rpc', 'AvtoDev\\JsonRpc\\Http\\Controllers\\RpcController');

如果您想指定不在 ./routes/web.php 文件中的路由,不要忘记加载 RPC 路由

创建 RPC 控制器

创建一个包含将被 JSON 请求调用的过程的控制器

<?php

namespace YourNamespace;

use AvtoDev\JsonRpc\Requests\RequestInterface;

class SomeController
{
    /**
     * Get sum of array.
     *
     * @param RequestInterface $request
     *
     * @return int
     */
    public function sum(RequestInterface $request): int
    {
        return (int) \array_sum($request->getParams());
    }

    /**
     * Get info from request.
     *
     * @param RequestInterface $request
     *
     * @return mixed[]
     */
    public function showInfo(RequestInterface $request): array
    {
        return [
            'params'       => $request->getParams(),
            'notification' => $request->isNotification(),
            'method_name'  => $request->getMethod(),
            'request ID'   => $request->getId(),
        ];
    }
}

发送 RPC 请求

$ curl 'https:///rpc' \
    -H 'Content-Type: application/json;charset=utf-8' \
    --data '{"jsonrpc":"2.0","method":"please_sum_array_values","id":"UNIQ_REQUEST_ID","params":[1,2,3,4,5,6,7,8,9,10]}'

{
    "jsonrpc": "2.0",
    "result": 55,
    "id": "UNIQ_REQUEST_ID"
}
$ curl 'https:///rpc' \
    -H 'Content-Type: application/json;charset=utf-8' \
    --data '{"jsonrpc":"2.0","method":"show_full_request","id":"UNIQ_REQUEST_ID","params":[1,2,3,4,5,6,7,8,9,10]}'

{
    "jsonrpc": "2.0",
    "result": {
        "params": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        "notification": false,
        "method_name": "please_sum_array_values",
        "request ID": "UNIQ_REQUEST_ID"
    },
    "id": "UNIQ_REQUEST_ID"
}
$ curl 'https:///rpc' \
    -H 'Content-Type: application/json;charset=utf-8' \
    --data '{"jsonrpc":"2.0","method":"undefined_method","id":"UNIQ_REQUEST_ID"}'

{
    "jsonrpc": "2.0",
    "error": {
        "code": -32601,
        "message": "Method not found"
    },
    "id": "UNIQ_REQUEST_ID"
}

事件

当调用 Kernel@handle 方法时,会触发一些事件

如果需要,您可以在该 事件 上创建一个 监听器 并显示收到的消息以进行调试。

与您的 Laravel 应用 一起提供的 EventServiceProvider 为注册所有应用程序的事件监听器提供了一个方便的位置。

变更日志

Release date Commits since latest release

变更日志可以在以下位置找到 这里

支持

Issues Issues

如果您在此包中发现任何错误,请在此存储库中 创建问题

许可证

这是一个开源软件,根据 MIT 许可证 许可。