PHP转账以太坊ETH余额-infura轻节点

发布于 2019-12-08 19:29:06

使用PHP查询以太坊ETH余额
开发前准备:
1.PHP版本需要大于7.1
2.需要有一个infura以太坊轻节点或者自己搭建的节点地址
(没有轻节点地址的可以点击下方链接去注册一个)
https://infura.io/

下面是以太坊rinkeby的测试网轻节点地址:
https://rinkeby.infura.io/v3/31090cb004d34600b113fa3e4203e9b5
下面是以太坊ETH主网的轻节点地址:
https://mainnet.infura.io/v3/31090cb004d34600b113fa3e4203e9b5

<?php


require_once "vendor/autoload.php";

use xtype\Ethereum\Client as EthereumClient;
use xtype\Ethereum\Utils;
/*
 * https://mainnet.infura.io/v3/31090cb004d34600b113fa3e4203e9b5
 *https://ropsten.infura.io/v3/31090cb004d34600b113fa3e4203e9b5
 *https://kovan.infura.io/v3/31090cb004d34600b113fa3e4203e9b5
 *https://rinkeby.infura.io/v3/31090cb004d34600b113fa3e4203e9b5
 *https://goerli.infura.io/v3/31090cb004d34600b113fa3e4203e9b5
 * */
//--关键数据
$from='0xAfE21e8915D98cC75715F113Bc0B24aC37D0670a';   //发送方
$address='0xA56F29c95540F392c83bD8eCcee961aBF3880Eb1'; //接收方
$num='0.01';   //发送金额
$PrivateKeys='6b7d092b258ff0322e06f5847b15b321ab696a3167c06cf3bd361e23005ad596';  //转账私钥


$client = new EthereumClient([
    'base_uri' => 'https://mainnet.infura.io/v3/a25b1f640f07417bbeed411fe7c5c8d7',
    'timeout' => 30,
]);
//var_dump($client);
echo "<pre>";
//print_r($client->eth_getBlockByNumber('0x' . dechex(5199525), false));

$client->addPrivateKeys([$PrivateKeys]);


// 2. 建立您的交易
$trans = [
    "from" => $from,
    "to" => $address,
    "value" => Utils::ethToWei($num, true),
    "data" => '0x',
];
// 你可以设定汽油,nonce,gasprice
$trans['gas'] = dechex(hexdec($client->eth_estimateGas($trans)) * 1.5);
//$trans['gasPrice'] = $client->eth_gasPrice();
$trans['gasPrice'] =  $client->eth_gasPrice();

$trans['nonce'] = $client->eth_getTransactionCount($from, 'pending');
// 3. 发送您的交易
// 如果需要服务器,也可以使用eth_sendTransaction
$txid = $client->sendTransaction($trans);
//var_dump($txid);
// 4. 如果没有错的话
// 你会在这里看到txid。喜欢 string(66) "0x1adcb80b413bcde285f93f0274e6cf04bc016e8813c8390ff31a6ccb43e75f51"
//print_r($txid);

// 5. 查看哈希情况
// https://ethereum.gitbooks.io/frontier-guide/content/rpc.html#eth_gettransactionreceipt
print_r($client->eth_getTransactionReceipt($txid));
print_r('<br>-----------------已发送到区块链-------------------<br>');
print_r('哈希值:'.$txid.'<br>');
print_r('发送地址:'.$from.'<br>');
print_r('接收地址:'.$address.'<br>');
print_r('发送金额:'.$num.'<br>');
print_r('------------------------------------');

使用PHP查询以太坊ETH余额教程结束

0 条评论

发布
问题