2017 © Pedro Peláez
 

library php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

image

denpa/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  • Sunday, July 15, 2018
  • by denpamusic
  • Repository
  • 8 Watchers
  • 60 Stars
  • 3,396 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 33 Forks
  • 1 Open issues
  • 25 Versions
  • 57 % Grown

The README.md

Simple Bitcoin JSON-RPC client based on GuzzleHttp

Latest Stable Version License Build Status Code Climate Code Coverage Join the chat at https://gitter.im/php-bitcoinrpc/Lobby, (*1)

Installation

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

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

and run php composer.phar install., (*3)

Requirements

PHP 7.1 or higher
For PHP 5.6 and 7.0 use php-bitcoinrpc v2.0.x., (*4)

Usage

Create new object with url as parameter, (*5)

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

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

/**
 * 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:, (*8)

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

You can also send requests using request method:, (*9)

/**
 * 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:, (*10)

$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:, (*11)

/**
 * 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., (*12)

to_bitcoin()

Converts value in satoshi to bitcoin., (*13)

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

to_satoshi()

Converts value in bitcoin to satoshi., (*14)

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

to_ubtc()

Converts value in bitcoin to ubtc/bits., (*15)

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

to_mbtc()

Converts value in bitcoin to mbtc., (*16)

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

to_fixed()

Trims float value to precision without rounding., (*17)

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

License

This product is distributed under MIT license., (*18)

Donations

If you like this project, please consider donating:
BTC: 3L6dqSBNgdpZan78KJtzoXEk9DN3sgEQJu
Bech32: bc1qyj8v6l70c4mjgq7hujywlg6le09kx09nq8d350, (*19)

❤Thanks for your support!❤, (*20)

The Versions

15/07 2018

dev-master

9999999-dev https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

15/07 2018

v2.0.4

2.0.4.0 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

15/07 2018

dev-analysis-zYkNKy

dev-analysis-zYkNKy https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

18/06 2018

v2.0.3

2.0.3.0 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

10/04 2018

2.x-dev

2.9999999.9999999.9999999-dev https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

06/04 2018

v2.0.2

2.0.2.0 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

06/04 2018

dev-analysis-zd21KL

dev-analysis-zd21KL https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

21/08 2017

v2.0.1

2.0.1.0 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

17/08 2017

v2.0.0

2.0.0.0 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

17/08 2017

v2.0.0rc1

2.0.0.0-RC1 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

15/08 2017

v2.0.0beta1

2.0.0.0-beta1 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

15/08 2017

dev-analysis-z4yy6N

dev-analysis-z4yy6N https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

14/08 2017

dev-analysis-zO2AyJ

dev-analysis-zO2AyJ https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

13/08 2017

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

12/08 2017

v1.0.8

1.0.8.0 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

12/08 2017

dev-analysis-XaNJgm

dev-analysis-XaNJgm https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

12/08 2017

v1.0.7

1.0.7.0 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

05/02 2017

v1.0.6

1.0.6.0 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

31/01 2017

v1.0.5

1.0.5.0 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

25/01 2017

dev-analysis-qrYLGG

dev-analysis-qrYLGG https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

21/01 2017

v1.0.4

1.0.4.0 https://github.com/denpamusic/php-bitcoinrpc

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

21/01 2017

v1.0.3

1.0.3.0 https://github.com/denpamusic/php-bitapi

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

21/01 2017

v1.0.2

1.0.2.0 https://github.com/denpamusic/php-bitapi

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

20/01 2017

v1.0.1

1.0.1.0 https://github.com/denpamusic/php-bitapi

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc

20/01 2017

v1.0.0

1.0.0.0 https://github.com/denpamusic/php-bitapi

Bitcoin JSON-RPC client based on GuzzleHttp

  Sources   Download

MIT

The Requires

 

The Development Requires

by Denis Paavilainen

api guzzle bitcoin jsonrpc