LoginSignup
3
0

More than 5 years have passed since last update.

【Bitcoin】PHP LaravelからBitcoinへのRPC接続方法

Last updated at Posted at 2018-05-06

1.前提

  • Laravelをインストールしている
  • Bitcoinを起動している

2.環境

  • 使用OS
    • Ubuntu v16.04.4
  • 使用言語
    • Bitcoin v0.16
    • PHP v7.2.4
      • Laravel v5.5.40

3.ライブラリのダウンロード

PHPからBitcoinへRPC接続を行うため、今回はEasyBitcoin-PHPというライブラリを使用していきます。
https://github.com/aceat64/EasyBitcoin-PHP

Laravelのディレクトリから以下のコマンドを叩いていきます。

lib-download
# こちらは環境に合わせて変更してください
$ cd <laravel project>

# Libs ディレクトリ作成
$ mkdir app/Libs

# easybitcoin.php のダウンロード
$ wget -O app/Libs/Bitcoin.php https://raw.githubusercontent.com/aceat64/EasyBitcoin-PHP/master/easybitcoin.php
$ vim app/Libs/Bitcoin.php
# namespace を記載
# namespace App¥Libs;

これでライブラリが使えるようになったので、続いて実装へ移っていきます。

4.PHP LaravelからBitcoinへのRPC接続

Bitcoinがregtestモードで起動している前提で進めていきます。

.env
### 上記省略 ###
BITCOIN_RPC_USERNAME=user
BITCOIN_RPC_PASSWORD=password
BITCOIN_RPC_HOST=localhost
# regtest default port
BITCOIN_RPC_PORT=18443
config/bitcoin.php
<?php

return [
    'bitcoin_rpc_username' => env('BITCOIN_RPC_USERNAME', 'user'),
    'bitcoin_rpc_password' => env('BITCOIN_RPC_PASSWORD', 'password'),
    'bitcoin_rpc_host' => env('BITCOIN_RPC_HOST', 'localhost'),
    # mainnet default port
    'bitcoin_rpc_port' => env('BITCOIN_RPC_PORT', '8956'),
];
routes/web.php
<?php

Route::get('/', 'IndexController@index');
app/Http/Controllers/IndexController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Config;

# 先ほどダウンロードしたライブラリをuseする
use App\Libs\Bitcoin;

class IndexController extends Controller
{
    public function index()
    {
        $bitcoin = new Bitcoin(
            # rpc username
            Config::get('bitcoin.rpc_username'),
            # rpc password
            Config::get('bitcoin.rpc_password'),
            # host
            Config::get('bitcoin.rpc_host'),
            # regtest port
            Config::get('bitcoin.rpc_port')
        );

        # getbalance() で残高を取得する
        print_r($bitcoin->getbalance());
    }
}

これでPHP側からBitcoinのRPCコマンドを叩くことができるようになりました。
あとはbitcoin-cliから叩くのと同じような形で使っていけばOKです。

よく使うコマンドについては別記事にまとめてありますので、こちらを見ながら色々なコマンドもお試しください!
bitcoin-cli でよく使うコマンドまとめ(ver 0.16.0)

sample.php
<?php
# listaccounts() でアカウントごとの残高を取得する
print_r($bitcoin->listaccounts());

# getaccountaddress() で引数のアカウントが持つビットコインアドレスを1つ取得する
print_r($bitcoin->getaccountaddress('アカウント名'));

# getaddressesbyaccount() で引数のアカウントが持つビットコインアドレスを全て取得する
print_r($bitcoin->getaddressesbyaccount('アカウント名'));

# getblock() で引数のブロックハッシュ値のブロックの中身を取得する
print_r($bitcoin->getblock('ブロックのハッシュ値'));

# gettransaction() で引数のトランザクションIDのトランザクションの中身を取得する
print_r($bitcoin->gettransaction('トランザクションID'));

# sendfrom() でコインを送金し、発行されたトランザクションIDを取得する
print_r($bitcoin->sendfrom('送金元アカウント名', '送金先アドレス', '送金額'));

5.参考文献

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0