1.Geth下載
(對以太坊了解不深的可以后面自己查找有關(guān)geth的資料,與此類似的客戶端還有Ethereum、Parity 、Mist),本文采用windows版(Windows系統(tǒng):geth-windows-amd64-1.8.3)
https://ethfans.org/wikis/Ethereum-Geth-Mirror
安裝:
windows版的直接傻瓜式安裝到放軟件的地方,然后打開安裝根目錄,這時可以看到有個geth.exe的可執(zhí)行文件,先別急執(zhí)行;先在該目錄下創(chuàng)建一個piccgenesis.json文件。
piccgenesis.json文件內(nèi)容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
{ "config" : { "chainId" : 33, "homesteadBlock" : 0, "eip155Block" : 0, "eip158Block" : 0 }, "coinbase" : "0x0000000000000000000000000000000000000000" , "difficulty" : "0x4" , "extraData" : "" , "gasLimit" : "0xffffffff" , "nonce" : "0x0000000000000042" , "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000" , "parentHash" : “0x0000000000000000000000000000000000000000000000000000000000000000 ", " timestamp ": " 0x00 ", " alloc": { } } |
配置說明請參考:https://www.colabug.com/4943842.html
現(xiàn)在使用cmd命令行工具來運(yùn)行g(shù)eth.exe,在該終端下執(zhí)行如下命令:
geth --datadir “chain” init piccgenesis.json
然后在該終端設(shè)置json-rpc,命令如下:
geth --rpc --rpccorsdomain * --datadir ./mychain -rpcport 8534 --port 30308 --identity test --networkid 111111111 --rpcaddr 0.0.0.0 --rpcapi admin,miner,db,eth,net,web3,personal --nodiscover console
注:這里用的是測試網(wǎng)絡(luò),不需要同步所有區(qū)塊,如果要正式應(yīng)用需要一臺linux服務(wù)器,開啟主網(wǎng)同步所有區(qū)塊,在打開并設(shè)置rpc。
2.使用composer下載web3.php
在此之前本人默認(rèn)你已有一個本地的測試項(xiàng)目(能跑通),本人用的是tp5的測試項(xiàng)目
要求:
php版本大于7.1且php需開啟openssl擴(kuò)展
安裝過程:
請先在composer中加入一行
“minimum-stability”: “dev”,
然后在項(xiàng)目根目錄下執(zhí)行(該項(xiàng)目根目錄必須)
composer require sc0vu/web3.php dev-master
或者在 composer.json中加入
“sc0vu/web3.php”: “dev-master”
到這里,準(zhǔn)備工作基本完畢。
PHP代碼調(diào)用web.php接口實(shí)現(xiàn)創(chuàng)建新的以太坊賬號(在該本地項(xiàng)目需要處理以太坊賬號管理邏輯的控制器寫,建議隨便找個控制器或php頁面先測試)
$web3 = new \Web3\Web3(‘http://localhost:8534'); //這里的端口是前面設(shè)置的rpc端口號,不要把geth終端關(guān)掉
$newAccount = ‘';
1
2
3
4
5
6
7
8
|
$web3 ->personal->newAccount( '123456' , function ( $err , $account ) use (& $newAccount ) { if ( $err !== null) { echo 'Error: ' . $err ->getMessage(); return ; } $newAccount = $account ; echo 'New account: ' . $account . PHP_EOL; }); |
執(zhí)行結(jié)果:
到這里,基本上php+eth的開發(fā)就可以跑起來了,下面是web3.php的一些接口使用
https://github.com/sc0Vu/web3.php
gethAPI文檔:http://cw.hubwiz.com/card/c/geth-rpc-api/1/4/5/
以上就是PHP實(shí)現(xiàn)創(chuàng)建以太坊錢包轉(zhuǎn)賬等功能的詳細(xì)內(nèi)容,更多關(guān)于PHP以太坊錢包的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://blog.csdn.net/song40188/article/details/86013244