hernandev / light-rpc

LightRPC 是一个为 PHP 设计的简洁、易用、简单的 HTTPS 上 JSON-RPC 客户端。

1.0.2 2018-04-10 05:14 UTC

This package is auto-updated.

Last update: 2024-08-25 04:29:56 UTC


README

Build Status Codecov Latest Stable Version License

LightRPC:PHP 的一个简单、直接且有效的 JSON-RPC 2 客户端。

此客户端的设计灵感来自 JavaScript 项目 LightRPC

1. 背景。

本项目的首要目标是与 STEEM 区块链 JSON-RPC 服务器通信。它足够简单,可以适应任何 JSON-RPC 2 服务,但默认值旨在简化 STEEM 的使用。

2. 安装

非常简单

composer require hernandev/light-rpc

3. 使用

非常简单,选择一个

3.1. 直接调用

// alias.
use LightRPC\Client;

// start a client instance.
$client = new Client('https://api.steemit.com');

// call it.
$response = $client->call('follow_api', 'get_follow_count', ['hernandev']);

3.2. 请求实例。

// alias.
use LightRPC\Client;
use LightRPC\Request;

// start a client instance.
$client = new Client('https://api.steemit.com');

// create a request instance.
$request = new Request('follow_api', 'get_follow_count', ['hernandev']);

// send it.
$response = $client->send($request);

3.3. 处理响应

非常简单,选择一个

// wanna check for errors?
$response->isError();


// use the magic result getters.
$response->account;           // 'hernandev'
$response->follower_count;    // 123
$response->following_count;   // 123

// OR

// use a get method:
$response->get('account');           // 'hernandev'
$response->get('follower_count');    // 123
$response->get('following_count');   // 123

// OR

// get all result OR error data:
$response->data();  // [ 'account' => 'hernandev', 'following_count' => 123, 'follower_count' => 123]
$response->get();   // [ 'account' => 'hernandev', 'following_count' => 123, 'follower_count' => 123]

// OR

// If you are a boring person, just get the full response as array.
$response->toArray(); // [ 'jsonrpc' => '2.0', 'id' => 0, 'result' => ['foo' => 'bar']]

// You are really boring you know, wanna as JSON string?
(string) $response; // '{"jsonrpc":"2.0","id":0,"result":{...}}