如何通过Geth、Node.js和UNIX/PHP访问以太坊节点

原创
2019/01/07 16:47
阅读数 471

本文旨在说明通过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.phpPHP脚本。
  • 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访问以太坊节点

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部