trifs/jsonrpc

简单的JSON-RPC客户端和服务器

v1.1.1 2017-10-16 07:55 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:03:17 UTC


README

PHP JSON-RPC是一个简单的JSON-RPC客户端和服务器。它目前处于alpha状态,预计很快会有一个稳定版本。警告:API也可能很快会发生变化:)

Build Status

安装

Composer

如果您使用Composer来管理项目的依赖项,请将trifs/phpjsonrpc添加到项目的composer.json文件中。以下是一个composer.json的最小示例

{
    "require": {
        "trifs/jsonrpc": "dev-master"
    }
}

要使用Composer进行系统级安装,可以运行

composer global require 'trifs/phpjsonrpc=*'

使用示例

客户端(单个请求)

$client = new trifs\jsonrpc\Client('http://example.com');
$client->request('method-one')
    ->send();

$client = new trifs\jsonrpc\Client('http://example.com');
$client->notification('method-one')
    ->send();

客户端(批量请求)

$client = new trifs\jsonrpc\Client('http://example.com');
$client->request('method-one')
    ->request('method-two')
    ->notification('method-three')
    ->send();

客户端配置

客户端可以通过额外的选项进行配置

  • timeout(秒),file_get_contents调用的连接超时。
$client = new trifs\jsonrpc\Client(
    'http://example.com',
    ['timeout' => 5, ]
);

还可以将file_get_contents调用与使用curl实现的实现进行交换

$client->setTransporter(new trifs\jsonrpc\Client\Transporter\CurlTransporter());

如果php.ini配置中的allow_url_fopen设置为Off,这将禁用远程URL的file_get_contents,则此选项可能很有用。

服务器

$input   = file_get_contents('php://input');
$invoker = function($method, array $params = []) {
    return time();
};

$server = new trifs\jsonrpc\Server($input, $invoker);
$server->run();

贡献

我们始终欢迎贡献。通过通过GitHub pull requests发送您的贡献,您可以让我们的生活变得更轻松。

由于时间限制,我们并不总是能像我们希望的那样快速回应。请不要将延误视为个人问题,如果您觉得我们忘记了回应,请随时在这里提醒我们。

在开发环境中使用PHP JSON-RPC

为了在本地设置PHP JSON-RPC,请确保已安装VagrantVirtualBox

git clone git://github.com/3fs/php-json-rpc
cd php-json-rpc
vagrant up

更改后,运行./build/qa.sh all,然后放松休息。如果有问题报告,请重复操作。如果没有,再试试看:)