LoginSignup
8
7

More than 5 years have passed since last update.

【Ethereum】PHP LaravelからEthereum(Geth)へのRPC接続方法

Last updated at Posted at 2018-05-13

1.前提

2.環境

  • 使用OS
    • Ubuntu 16.04.4
  • 使用言語
    • PHP v7.2.4
      • Laravel 5.5.40
    • geth v1.8.2

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

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

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

download
$ cd <laravel project>
$ mkdir app/Libs
$ mkdir app/Libs/Eth
$ cd app/Libs/Eth

# ethereum.php のダウンロード
$ wget -O Ethereum.php https://raw.githubusercontent.com/btelle/ethereum-php/master/ethereum.php
$ vim Ethereum.php
# namespace を記載
# namespace App¥Libs¥Eth

# json-rpc.php のダウンロード
$ wget -O JSON-RPC.php https://raw.githubusercontent.com/btelle/ethereum-php/master/json-rpc.php
$ vim JSON-RPC.php
# namespace App¥Libs¥Eth

4.PHP LaravelからEthereum(Geth)への接続

今回はGethをプライベートネットワークで起動している前提で進めていきます。

.env
### 上記省略 ###
ETH_NODE_HOST=localhost
ETH_NODE_PORT=8545
config/eth.php
<?php

return [
    'node_host' => env('ETH_NODE_HOST', 'localhost'),
    'node_port' => env('ETH_NODE_PORT', '8545'),
];
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\Eth\Ethereum;

class IndexController extends Controller
{
    public function index()
    {
        $eth = new Ethereum(Config::get('eth.node_host'), Config::get('eth.node_port'));

        dd($eth->eth_accounts());
    }
}

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

5.参考文献

8
7
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
8
7