denpa / php-bitcoinrpc
基于 GuzzleHttp 的 Bitcoin JSON-RPC 客户端
v2.2.0
2022-05-24 01:14 UTC
Requires
- php: >=8.0
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-24 06:46:45 UTC
README
安装
在您的项目目录中运行 php composer.phar require denpa/php-bitcoinrpc
,或者将以下行添加到 composer.json 中
"require": { "denpa/php-bitcoinrpc": "^2.2" }
然后运行 php composer.phar install
。
需求
PHP 8.0 或更高版本
对于 PHP 5.6 到 7.0,请使用 php-bitcoinrpc v2.0.x。
对于 PHP 7.0 到 7.4,请使用 php-bitcoinrpc v2.0.x。
用法
使用 URL 作为参数创建新对象
/** * Don't forget to include composer autoloader by uncommenting line below * if you're not already done it anywhere else in your project. **/ // require 'vendor/autoload.php'; use Denpa\Bitcoin\Client as BitcoinClient; $bitcoind = new BitcoinClient('http://rpcuser:rpcpassword@localhost:8332/');
或使用数组定义您的 bitcoind 设置
/** * Don't forget to include composer autoloader by uncommenting line below * if you're not already done it anywhere else in your project. **/ // require 'vendor/autoload.php'; use Denpa\Bitcoin\Client as BitcoinClient; $bitcoind = new BitcoinClient([ 'scheme' => 'http', // optional, default http 'host' => 'localhost', // optional, default localhost 'port' => 8332, // optional, default 8332 'user' => 'rpcuser', // required 'password' => 'rpcpassword', // required 'ca' => '/etc/ssl/ca-cert.pem', // optional, for use with https scheme 'preserve_case' => false, // optional, send method names as defined instead of lowercasing them ]);
然后使用魔法调用在 Bitcoin Core API 文档 中定义的方法
/** * Get block info. */ $block = $bitcoind->getBlock('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'); $block('hash')->get(); // 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f $block['height']; // 0 (array access) $block->get('tx.0'); // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b $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 = $bitcoind->sendToAddress('mmXgiR6KAhZCyQ8ndr2BCfEq1wNG2UnyG6', 0.1); $txid = $result->get(); /** * Get transaction amount. */ $result = $bitcoind->listSinceBlock(); $bitcoin = $result->sum('transactions.*.amount'); $satoshi = \Denpa\Bitcoin\to_satoshi($bitcoin);
要发送异步请求,请将 Async 添加到方法名称中
$bitcoind->getBlockAsync( '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', function ($response) { // success }, function ($exception) { // error } );
您还可以使用请求方法发送请求
/** * Get block info. */ $block = $bitcoind->request('getBlock', '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'); $block('hash'); // 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f $block['height']; // 0 (array access) $block->get('tx.0'); // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b $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 = $bitcoind->request('sendtoaddress', 'mmXgiR6KAhZCyQ8ndr2BCfEq1wNG2UnyG6', 0.06); $txid = $result->get();
或使用 requestAsync 方法进行异步调用
$bitcoind->requestAsync( 'getBlock', '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', function ($response) { // success }, function ($exception) { // error } );
多钱包 RPC
您可以使用 wallet($name)
函数进行 多钱包 RPC 调用
/** * Get wallet2.dat balance. */ $balance = $bitcoind->wallet('wallet2.dat')->getbalance(); echo $balance->get(); // 0.10000000
异常
Denpa\Bitcoin\Exceptions\BadConfigurationException
- 在客户端配置错误时抛出。Denpa\Bitcoin\Exceptions\BadRemoteCallException
- 在从守护程序获取错误消息时抛出。Denpa\Bitcoin\Exceptions\ConnectionException
- 在守护程序连接错误(例如超时)时抛出
辅助工具
此包提供以下辅助工具来协助处理值。
to_bitcoin()
将 satoshi 中的值转换为比特币。
echo Denpa\Bitcoin\to_bitcoin(100000); // 0.00100000
to_satoshi()
将比特币中的值转换为 satoshi。
echo Denpa\Bitcoin\to_satoshi(0.001); // 100000
to_ubtc()
将比特币中的值转换为 ubtc/bits。
echo Denpa\Bitcoin\to_ubtc(0.001); // 1000.0000
to_mbtc()
将比特币中的值转换为 mbtc。
echo Denpa\Bitcoin\to_mbtc(0.001); // 1.0000
to_fixed()
修剪浮点值到无舍入的精度。
echo Denpa\Bitcoin\to_fixed(0.1236, 3); // 0.123
许可证
本产品在 MIT 许可证下分发。
捐赠
如果您喜欢这个项目,请考虑捐赠
BTC: 3L6dqSBNgdpZan78KJtzoXEk9DN3sgEQJu
Bech32: bc1qyj8v6l70c4mjgq7hujywlg6le09kx09nq8d350
❤感谢您的支持!❤