vaderangry/php-json-rpc

适用于 PHP7 的灵活 JSON-RPC2 服务器/客户端实现

v1.2.1 2018-03-02 13:10 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:25:22 UTC


README

适用于 PHP7 的灵活 JSON-RPC2 服务器/客户端实现。

Scrutinizer Code Quality Build Status

功能

  • 完全符合 JSON-RPC 2.0 规范(批量请求、通知、位置参数和命名参数等)。
  • 使用默认路由快速启动,基于 PHP 命名空间。
  • 根据需求灵活自定义路由。
  • 通过处理器拦截请求和响应的机制。
  • 请求和响应中的自动类型转换。
  • 全面单元测试。

安装

打开命令行,进入您的项目目录,并执行以下命令以下载此捆绑包的最新稳定版本

$ composer require vaderangry/php-json-rpc

此命令要求您全局安装了 Composer,请参阅 Composer 文档中的 安装章节 以了解详细信息。

文档

基本使用

服务器

快速启动示例

<?php

use PhpJsonRpc\Server;

// Class for which provide JSON-RPC2 API:
class Math
{
    public function pow(float $base, int $exp): float
    {
        return pow($base, $exp);
    }
}

$server = new Server();

$response = $server
    ->addHandler(new Math())
    ->execute();

echo $response;

默认情况下,方法 Math::pow 将映射到 JSON-RPC2 术语中的方法 Math.pow。请求示例

{
  "jsonrpc": "2.0", 
  "method": "Math.pow", 
  "params": {"base": 2, "exp": 3}, 
  "id": 1
}

自定义方法映射示例

<?php

use PhpJsonRpc\Server;
use PhpJsonRpc\Server\MapperInterface;

// Define custom mapper
class Mapper implements MapperInterface
{
    public function getClassAndMethod(string $requestedMethod): array
    {
        // Keys of array presents requested method
        $map = [
            'pow' => [Math::class, 'pow'],
        ];

        if (array_key_exists($requestedMethod, $map)) {
            return $map[$requestedMethod];
        }

        return ['', ''];
    }
}

$server = new Server();

// Register new mapper
$server->setMapper(new Mapper());

// Register handler and run server
$response = $server->addHandler(new Math())->execute();

echo $response;

现在 Math::pow 将映射到 pow。请求示例

{
  "jsonrpc": "2.0", 
  "method": "pow", 
  "params": {"base": 2, "exp": 3}, 
  "id": 1
}

客户端

单个请求

<?php

use PhpJsonRpc\Client;

$client = new Client('https://');
$result = $client->call('Math.pow', [2, 3]); // $result = 8
$result = $client->call('Math.methodNotFound', []); // $result = null

单个请求(异常服务器错误模式)

<?php

use PhpJsonRpc\Client;

$client = new Client('https://', Client::ERRMODE_EXCEPTION);
$result = $client->call('Math.methodNotFound', []); // throw MethodNotFoundException

批量请求

<?php

use PhpJsonRpc\Client;

$client = new Client('https://');

$result = $client->batch()
    ->call('Util.Math.pow', [2, 1])
    ->call('Util.Math.pow', [2, 2])
    ->call('Util.Math.pow', [2, 3])
    ->call('Util.methodNotFound', [])
    ->batchExecute();
// $result = [2, 4, 8, null]

所有结果单元都存储在调用相同的位置。服务器错误以 null 对象的形式出现。

异常服务器错误模式的批量请求

<?php

use PhpJsonRpc\Client;

$client = new Client('https://', Client::ERRMODE_EXCEPTION);

$result = $client->batch()
    ->call('Util.Math.pow', [2, 1])
    ->call('Util.methodNotFound', [])
    ->batchExecute();
// $result = [2, MethodNotFoundException]

在异常模式下,服务器错误以 JsonRpcException 对象的形式出现。异常不会抛出以保存其他结果单元。

请求中包含个人自定义头的示例

<?php

use PhpJsonRpc\Client;
use PhpJsonRpc\Common\Interceptor\Container;
use PhpJsonRpc\Common\Interceptor\Interceptor;

$client = new Client('https://');

$client->getTransport()->onPreRequest()
   ->add(Interceptor::createWith(function (Container $container) {
        // Get transport from container
        $transport = $container->first();

        // Add required headers
        $transport->addHeaders([
            "Origin: " . $_SERVER['HTTP_HOST'],
        ]);

        // Now we MUST return container for next chain
        return new Container($transport, $container->last());
    }));
    
$result = $client->call('Math.pow', [2, 3]); // $result = 8

测试

$ ./vendor/bin/phpunit -c ./