majestic/php-litecoinrpc

基于 GuzzleHttp 的 Litecoin JSON-RPC 客户端

v2.0.2 2017-10-19 20:32 UTC

README

关于

该项目基于 php-bitcoinrpc 项目 - 完全单元测试过的 Litecoin JSON-RPC 客户端,由 GuzzleHttp 驱动。

安装

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

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

然后运行 php composer.phar update

要求

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

使用方法

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

use Majestic\Litecoin\Client as LitecoinClient;

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

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

use Majestic\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 Majestic\Litecoin\LitecoindResponse;

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

$promise->wait();

您还可以使用 request 方法

/**
 * 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 = $BTC->request('sendtoaddress', ['LKdsQGCwBbgJNdXSQtAvVbFMpwgwThtsSY', 0.06]);
$txid = $result->get();

或 requestAsync 方法进行异步调用

use Majestic\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 许可证分发。

捐赠

如果您喜欢这个项目,您可以向 LKdsQGCwBbgJNdXSQtAvVbFMpwgwThtsSY 捐赠 Litecoin。

感谢您的支持!