azartpay/azart-rpc-php

基于 GuzzleHttp 的 Azart JSON-RPC 客户端

2.0.3 2018-06-05 04:20 UTC

README

简单的基于 GuzzleHttp 的 Azart JSON-RPC 客户端

安装

在项目目录中运行 php composer.phar require azartpay/azart-rpc-php,或者在 composer.json 中添加以下行

"require": {
    "azartpay/azart-rpc-php": "2.0.3"
}

然后运行 php composer.phar update

要求

PHP 7.0 或更高版本(也应该能在 5.6 上运行,但不予支持)

用法

使用 url 作为参数创建新对象

use AzartPay\Azart\Client as AzartClient;

$azartd = new AzartClient('http://rpcuser:rpcpassword@localhost:9798/');

或者使用数组定义您的 azartd 设置

use AzartPay\Azart\Client as AzartClient;

$azartd = new AzartClient([
    'scheme' => 'http',                 // optional, default http
    'host'   => 'localhost',            // optional, default localhost
    'port'   => 9798,                   // optional, default 9798
    'user'   => 'rpcuser',              // required
    'pass'   => 'rpcpassword',          // required
    'ca'     => '/etc/ssl/ca-cert.pem'  // optional, for use with https scheme
]);

然后使用魔法调用在 Dash Core API 文档 中定义的方法

/**
 * Get block info.
 */
$block = $azartd->getBlock('000009b9903dae4466d48db6c264d711ac554492da34cd0bfa4c0b6d230f29c9');

$block('hash')->get();     // 000009b9903dae4466d48db6c264d711ac554492da34cd0bfa4c0b6d230f29c9
$block['height'];          // 0 (array access)
$block->get('tx.0');       // 44701bbc011bdd471b75fa83e42acc8e067759a69cdeef723df57181a33e5467
$block->count('tx');       // 1
$block->has('version');    // key must exist and CAN NOT be null
$block->exists('version'); // key must exist and CAN be null
$block->contains(0);       // check if response contains value
$block->values();          // array of values
$block->keys();            // array of keys
$block->random(1, 'tx');   // random block txid
$block('tx')->random(2);   // two random block txid's
$block('tx')->first();     // txid of first transaction
$block('tx')->last();      // txid of last transaction

/**
 * Send transaction.
 */
$result = $azartd->sendToAddress('AqUM31KtkxgbMwYrrpUi6RVjaftK3Mv5mG', 0.1);
$txid = $result->get();

/**
 * Get transaction amount.
 */
$result = $azartd->listSinceBlock();
$totalAmount = $result->sum('transactions.*.amount');
$totalSatoshi = AzartClient::toSatoshi($totalAmount);

要发送异步请求,将 Async 添加到方法名称中

use AzartPay\Azart\AzartdResponse;

$promise = $azartd->getBlockAsync(
    '000009b9903dae4466d48db6c264d711ac554492da34cd0bfa4c0b6d230f29c9',
    function (AzartdResponse $success) {
        //
    },
    function (\Exception $exception) {
        //
    }
);

$promise->wait();

您还可以使用请求方法

/**
 * Get block info.
 */
$block = $azartd->request('getBlock', '000009b9903dae4466d48db6c264d711ac554492da34cd0bfa4c0b6d230f29c9');

$block('hash');            // 000009b9903dae4466d48db6c264d711ac554492da34cd0bfa4c0b6d230f29c9
$block['height'];          // 0 (array access)
$block->get('tx.0');       // 44701bbc011bdd471b75fa83e42acc8e067759a69cdeef723df57181a33e5467
$block->count('tx');       // 1
$block->has('version');    // key must exist and CAN NOT be null
$block->exists('version'); // key must exist and CAN be null
$block->contains(0);       // check if response contains value
$block->values();          // get response values
$block->keys();            // get response keys
$block->first('tx');       // get txid of the first transaction
$block->last('tx');        // get txid of the last transaction
$block->random(1, 'tx');   // get random txid

/**
 * Send transaction.
 */
$result = $azartd->request('sendtoaddress', ['AqUM31KtkxgbMwYrrpUi6RVjaftK3Mv5mG', 0.06]);
$txid = $result->get();

或请求Async方法进行异步调用

use AzartPay\Azart\AzartdResponse;

$promise = $azartd->requestAsync(
    'getBlock',
    '000009b9903dae4466d48db6c264d711ac554492da34cd0bfa4c0b6d230f29c9',
    function (AzartdResponse $success) {
        //
    },
    function (\Exception $exception) {
        //
    }
);

$promise->wait();

许可

本产品在 MIT 许可下分发。