本文旨在說明通過Geth、Node.js如何訪問以太坊節點和UNIX下PHP如何訪問以太坊節點。
說明如何通過RPC使用此(A)以太坊節點
對於以太坊主網路使用RPC url:http://85.214.51.53:8545
對於Ropsten測試網路使用RPC url:http://85.214.51.53:8546
通過本地系統上的“geth”訪問節點:
- 1.確保你的本地IP已在你的使用者配置檔案中註冊。使用RPC服務時,不會阻止從此IP訪問。
- 2.在本地系統上安裝
geth
二進位制檔案約30MB,geth二進位制檔案包。 - 3.查詢
geth
目錄位置 - 4.使用命令
./geth attach rpc:http://85.214.51.53:8545
連線並啟動JavaScript環境,參考文件。 - 5.控制檯正在啟動,你會看到
>
作為輸入提示符。 - 6.寫
eth.getBlock(10000)
並獲取塊資訊作為結果,參考文件。
你的系統在幾分鐘內執行,無需安裝已經500GB以上的以太坊區塊鏈!
故障排除:
- 安裝正確的geth二進位制檔案。
- 驗證geth在本地系統上是否可用。
- 檢查使用者配置檔案中的IP。
- 確認沒有個人防火牆阻止你的傳送出去請求。
嘗試Ropsten測試網路:
通過本地系統或伺服器上的node.js訪問節點
- 1.這將返回一個webserver響應並輸出一個html檔案。
- 2.在node.js系統中通過NPM或其他方式安裝web3。
- 3.將伺服器檔案指向app.js。
這將為你帶來一個帳戶餘額(錢包)的輸出。
檔案:app.js
const http = require('http');
const walletAddress = '0xDED5f23C157aCef931946D9A695cAc3eF1AaaA8D';
var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://85.214.51.53:8545'));
var balance = web3.eth.getBalance('0xDED5f23C157aCef931946D9A695cAc3eF1AaaA8D');
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end("<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>ethernode.biz</title>\n" +
"</head>\n" +
"<body>\n" +
"<p>ethernode.biz</p>" +
"<center>\n" +
"</center>\n " +
"<p> Ethereum balance : " + balance +"</p>\n" +
"</body>\n" +
"</html>\n");
}).listen(process.env.PORT);
在Unix系統上通過PHP訪問節點
- 1.確保你的geth通過RPC正常工作。
- 2.在
geth_rpc_getblock.js
下的Unix系統上儲存以下shell指令碼。確保你具有執行它的正確許可權。 - 3.在指令碼中調整geth命令的路徑。
- 4.檢查命令
./geth_rpc_getblock.js
是否會帶來一些正確的輸出。它應返回類似Data::QBIT=27867000000;WETH=0;ETH=0.002650772
的內容。 - 5.將下面儲存在同一目錄下的
geth_access.php
PHP指令碼。 - 6.將
$shellstring
中的路徑調整為指令碼檔案位置。 - 7.php指令碼應該回顯指令碼的輸出。
Data::QBIT=27867000000;WETH=0;ETH=0.002650772
。
你說對了!請記住,從長遠來看,使用php shell_exec
命令訪問geth並不是很省事。
檔案:geth_rpc_getblock.js
#!/bin/sh
# ------------------------------------------------------------------------------
# Qubitica.net
# ------------------------------------------------------------------------------
# Don't define PARAM to use the standard IPC comms
# Use below to connect to RPC port
PARAM="rpc:http://85.214.51.53:8545"
/var/www/vhosts/ethernode.info/php/geth attach $PARAM << EOF | grep "Data:" | sed "s/Data: //"
var erc20ABI = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burn","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_value","type":"uint256"}],"name":"burnFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"},{"name":"_extraData","type":"bytes"}],"name":"approveAndCall","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"}];
//var QBITWalletAddress = "$1"; You may pass a parameter from the php call into this $1.
var QBITContractAddress1 = "0xCb5ea3c190d8f82DEADF7ce5Af855dDbf33e3962";
var QBITContractAddress2 = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
var QBITWalletAddress = "0x842286ea00502f8e5b1ea40d17ebc4b70becda08";
var QBITContract1= web3.eth.contract(erc20ABI).at(QBITContractAddress1);
var QBITContract2= web3.eth.contract(erc20ABI).at(QBITContractAddress2);
var acctBal = web3.fromWei(eth.getBalance(QBITWalletAddress), "ether");
totalBal += parseFloat(acctBal);
var QBITbalanceOf = QBITContract1.balanceOf(QBITWalletAddress);
var WETHbalanceOf = QBITContract2.balanceOf(QBITWalletAddress);
console.log("Data:"+":QBIT="+ QBITbalanceOf +";WETH="+ WETHbalanceOf +";ETH="+ acctBal);
exit;
<<<<< New File
PHP指令碼:geth_access.php
<?php
$shellstring= "./geth_rpc_getblock.js";
$output=shell_exec($shellstring);
echo "$output";
?>
故障排除
- shell指令碼對Windows換行符很敏感或返回
\n\lf
- 檢查許可權
安利比特幣、以太坊的區塊鏈線上互動式程式設計實戰教程:
php以太坊,主要是介紹使用php進行智慧合約開發互動,進行賬號建立、交易、轉賬、代幣開發以及過濾器和交易等內容。
php比特幣開發教程,本課程面向初學者,內容即涵蓋比特幣的核心概念,例如區塊鏈儲存、去中心化共識機制、金鑰與指令碼、交易與UTXO等,同時也詳細講解如何在Php程式碼中整合比特幣支援功能,例如建立地址、管理錢包、構造裸交易等,是Php工程師不可多得的比特幣開發學習課程。
。
匯智網原創翻譯,轉載請標明出處。這裡是原文如何通過Geth、Node.js和UNIX/PHP訪問以太坊節點
本作品採用《CC 協議》,轉載必須註明作者和本文連結