jalallinux/php-json-rpc-client

此软件包帮助开发者轻松连接到RPC服务器。

1.1.0 2022-11-24 17:49 UTC

This package is auto-updated.

Last update: 2024-09-08 11:48:28 UTC


README

Latest Version on Packagist Tests Total Downloads

此软件包帮助开发者轻松连接到RPC服务器。

安装

您可以通过composer安装此软件包

composer require jalallinux/php-json-rpc-client

用法

创建实例

支持的选项: https://docs.guzzlephp.org/en/stable/request-options.html#auth

$rpc = new RpcClient('https://:8000/rpc/server', '2.0');

默认值

  • RPC版本:2.0

  • 选项

{
    "http_errors": false,
    "headers": {
      "Content-Type": "application/json",
      "Accept": "application/json"
    }
}

设置选项

$rpc->setOption('connect_timeout', 3.14);

设置头信息

$rpc->withHeaders(['api-key' => 'php-json-rpc-client-api-key']);

设置基本认证凭据

$rpc->withBasicAuth(['username', 'password']);

设置JWT授权令牌

$rpc->withJwtAuth('Bearer php-json-rpc-client-jwt-token');

添加RPC请求

$rpc->request('user.get', ['username' => 'jalallinux']);
$rpc->request('user.get', ['username' => 'jalallinux'], '1');

添加RPC通知

描述: https://www.jsonrpc.org/specification#notification

$rpc->notify('user.get', ['username' => 'jalallinux']);

发送最终请求

$rpc->send();

完整示例

$rpc = new RpcClient('https://:8000/rpc/server', '2.0');

$response = $rpc->setOption('connect_timeout', 3.14);
                ->withHeaders(['api-key' => 'php-json-rpc-client-api-key']);
                ->withBasicAuth(['username', 'password']);
                ->withJwtAuth('Bearer php-json-rpc-client-jwt-token');
                ->request('user.get', ['username' => 'jalallinux']);
                ->request('user.get', ['username' => 'jalallinux'], '1');
                ->notify('user.get', ['username' => 'jalallinux']);
                ->send();

响应方法

$response->body(): string
$response->array(): array
$response->object(): object
$response->status(): int
$response->successful(): bool
$response->ok(): bool
$response->failed(): bool
$response->clientError(): bool
$response->serverError(): bool
$response->header(string $header): string
$response->headers(): array

测试

composer test

变更日志

请查看 CHANGELOG 获取最近更改的更多信息。

安全漏洞

请查看 我们的安全策略 了解如何报告安全漏洞。

致谢

许可

MIT许可(MIT)。请参阅 许可文件 获取更多信息。