growlot/php-litecoinrpc

基于 GuzzleHttp 的 Litecoin JSON-RPC 客户端

v2.0.3 2019-06-23 17:46 UTC

This package is auto-updated.

Last update: 2024-09-24 10:10:11 UTC


README

关于

本项目基于 php-litecoinrpc 项目 - 由 GuzzleHttp 驱动的完整单元测试 Litecoin JSON-RPC 客户端。

安装

在您的项目目录中运行 php composer.phar require growlot/php-litecoinrpc,或者在 composer.json 中添加以下行

"require": {
    "growlot/php-litecoinrpc": "^2.0"
}

然后运行 php composer.phar update

要求

PHP 7.0 或更高版本(理论上也应适用于 5.6,但此版本不受支持)

用法

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

use Growlot\Litecoin\Client as LitecoinClient;

$litecoind = new LitecoinClient('http://rpcuser:rpcpassword@localhost:9332/');

或使用数组定义您的 litecoind 设置

use Growlot\Litecoin\Client as LitecoinClient;

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

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

/**
 * Get block info.
 */
$block = $litecoind->getBlock('9d4d9fd2f4dee46d5918861b7bbff81f52c581c3b935ad186fe4c5b6dc58d2f8');

$block('hash')->get();     // 9d4d9fd2f4dee46d5918861b7bbff81f52c581c3b935ad186fe4c5b6dc58d2f8
$block['height'];          // 1298009 (array access)
$block->get('tx.0');       // a8971eaf8dfda3ee5dd20b3de3fb6c22e936339bbb53f8fa0f2379941ac5ff3f
$block->count('tx');       // 26
$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 = $litecoind->sendToAddress('LKdsQGCwBbgJNdXSQtAvVbFMpwgwThtsSY', 0.1);
$txid = $result->get();

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

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

use Growlot\Litecoin\LitecoindResponse;

$promise = $litecoind->getBlockAsync(
    '9d4d9fd2f4dee46d5918861b7bbff81f52c581c3b935ad186fe4c5b6dc58d2f8',
    function (LitecoindResponse $success) {
        //
    },
    function (\Exception $exception) {
        //
    }
);

$promise->wait();

您也可以使用请求方法发送请求

/**
 * Get block info.
 */
$block = $litecoind->request('getBlock', '9d4d9fd2f4dee46d5918861b7bbff81f52c581c3b935ad186fe4c5b6dc58d2f8');

$block('hash');            // 9d4d9fd2f4dee46d5918861b7bbff81f52c581c3b935ad186fe4c5b6dc58d2f8
$block['height'];          // 1298009 (array access)
$block->get('tx.0');       // a8971eaf8dfda3ee5dd20b3de3fb6c22e936339bbb53f8fa0f2379941ac5ff3f
$block->count('tx');       // 26
$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->random(1, 'tx');   // get random txid

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

或使用 requestAsync 方法进行异步调用

use Growlot\Litecoin\LitecoindResponse;

$promise = $litecoind->requestAsync(
    'getBlock',
    '9d4d9fd2f4dee46d5918861b7bbff81f52c581c3b935ad186fe4c5b6dc58d2f8',
    function (LitecoindResponse $success) {
        //
    },
    function (\Exception $exception) {
        //
    }
);

$promise->wait();

多钱包 RPC

您可以使用 wallet($name) 函数执行 多钱包 RPC 调用

/**
 * Get wallet2.dat balance.
 */
$balance = $litecoind->wallet('wallet2.dat')->getbalance();

$balance->get(); // 0.10000000

许可证

本产品遵循 MIT 许可证分发。

捐赠

如果您喜欢此项目,您可以向 LbKRh7icJy8En3MDcPsxhLhF9quH9VNrgS 发送 Litecoin 进行捐赠。

感谢您的支持!