当前位置: 首页 > 软件库 > Web3 > 开源货币/比特币 >

php-bitcoinrpc

授权协议 MIT License
开发语言 Python
所属分类 Web3、 开源货币/比特币
软件类型 开源软件
地区 不详
投 递 者 赵景曜
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Simple Bitcoin JSON-RPC client based on GuzzleHttp

Installation

Run php composer.phar require denpa/php-bitcoinrpc in your project directory or add following lines to composer.json

"require": {
    "denpa/php-bitcoinrpc": "^2.1"
}

and run php composer.phar install.

Requirements

PHP 7.1 or higher
For PHP 5.6 and 7.0 use php-bitcoinrpc v2.0.x.

Usage

Create new object with url as parameter

/**
 * 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/');

or use array to define your bitcoind settings

/**
 * 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
]);

Then call methods defined in Bitcoin Core API Documentation with magic:

/**
 * 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);

To send asynchronous request, add Async to method name:

$bitcoind->getBlockAsync(
    '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
    function ($response) {
        // success
    },
    function ($exception) {
        // error
    }
);

You can also send requests using request method:

/**
 * 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();

or requestAsync method for asynchronous calls:

$bitcoind->requestAsync(
    'getBlock',
    '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
    function ($response) {
        // success
    },
    function ($exception) {
        // error
    }
);

Multi-Wallet RPC

You can use wallet($name) function to do a Multi-Wallet RPC call:

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

echo $balance->get(); // 0.10000000

Exceptions

  • Denpa\Bitcoin\Exceptions\BadConfigurationException - thrown on bad client configuration.
  • Denpa\Bitcoin\Exceptions\BadRemoteCallException - thrown on getting error message from daemon.
  • Denpa\Bitcoin\Exceptions\ConnectionException - thrown on daemon connection errors (e. g. timeouts)

Helpers

Package provides following helpers to assist with value handling.

to_bitcoin()

Converts value in satoshi to bitcoin.

echo Denpa\Bitcoin\to_bitcoin(100000); // 0.00100000

to_satoshi()

Converts value in bitcoin to satoshi.

echo Denpa\Bitcoin\to_satoshi(0.001); // 100000

to_ubtc()

Converts value in bitcoin to ubtc/bits.

echo Denpa\Bitcoin\to_ubtc(0.001); // 1000.0000

to_mbtc()

Converts value in bitcoin to mbtc.

echo Denpa\Bitcoin\to_mbtc(0.001); // 1.0000

to_fixed()

Trims float value to precision without rounding.

echo Denpa\Bitcoin\to_fixed(0.1236, 3); // 0.123

License

This product is distributed under MIT license.

Donations

If you like this project, please consider donating:
BTC: 3L6dqSBNgdpZan78KJtzoXEk9DN3sgEQJu
Bech32: bc1qyj8v6l70c4mjgq7hujywlg6le09kx09nq8d350

Thanks for your support!

 相关资料
  • 问题内容: 我被指示使用该方法,而不是与JQuery的Ajax请求进行交互时使用。我不了解使用vs 或全局方法的好处。 问题答案: 原因是无论内容类型如何,都将在请求的HTTP标头之后返回所有原始数据。 PHP superglobal 仅 应 包装以下任一数据 (用于简单表单发布的标准内容类型)或 (主要用于文件上传) 这是因为这些是用户代理 必须 支持的唯一内容类型。因此,服务器和PHP传统上不

  • PHP

    因此,我有一个带有注释功能的站点,其中注释的时间戳存储在MySQL数据库中。据我所知,时间戳在存储时转换为UTC,然后在检索时转换回默认时区。在我的例子中,我的服务器位于中央夏令时时区(CDT)。 我计划通过输入表单从每个用户那里获取时区。我只是想知道如何将时间戳值转换成用户的时区。 首先,我会将UTC转换为本地时区吗?或CDT到本地时区? 其次,我将如何在PHP中实现这一点?我只想: ...还是

  • php

    php tags allow php to be embedded directly into the template. They will not be escaped, regardless of the $php_handling setting. This is for advanced users only, not normally needed. php 标签允许在模板中直接嵌入

  • PHP

    PHP 网站应用如果是用 PHP 语言编写的,对 PHP 文件的请求需要用到 PHP 的解释器,它跟 Web 服务器之间需要用到 PHP-FPM。Web 服务器会把请求转发给 PHP-FPM,PHP-FPM 会使用 PHP 解释器处理请求,把结果再交给 Web 服务器。 搜索: yum search php 要安装的 PHP 的版本有很多选择,你得根据要运行的网站的需求,去安装网站应用推荐使用的

  • php

    请参考:http://www.kancloud.cn/manual/thinkphp/1819

  • PHP

    PHP(Hypertext Preprocessor)即“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页 目录结构CentOS /etc/php.ini: 配置文件,用来配置文件上传大小、内存、执行时间等 /etc/php-fpm.conf: PHP-FPM 配置文件