Skip to content

Instantly share code, notes, and snippets.

@daiki44
Last active June 19, 2018 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daiki44/cd9d9291d9cf598038ada9b339149e5b to your computer and use it in GitHub Desktop.
Save daiki44/cd9d9291d9cf598038ada9b339149e5b to your computer and use it in GitHub Desktop.
【Bitcoin】PHP LaravelからBitcoinへのRPC接続方法 ref: https://qiita.com/daiki_44/items/5cfaef3200b557308735
### 上記省略 ###
BITCOIN_RPC_USERNAME=user
BITCOIN_RPC_PASSWORD=password
BITCOIN_RPC_HOST=localhost
# regtest default port
BITCOIN_RPC_PORT=18443
<?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'),
];
<?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());
}
}
# こちらは環境に合わせて変更してください
$ 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;
<?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('送金元アカウント名', '送金先アドレス', '送金額'));
<?php
Route::get('/', 'IndexController@index');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment