:2026-04-08 8:45 点击:1
随着区块链技术的飞速发展,Web3 时代正加速到来,越来越多的传统应用开始集成区块链功能,其中以太坊作为最知名的智能合约平台,其转账功能是最基础且常用的需求之一,对于 PHP 开发者而言,如何利用 PHP 语言与以太坊网络交互,实现安全的转账接口,是一个重要的课题,本文将详细介绍如何使用 PHP 构建以太坊 Web3 转账接口,涵盖核心概念、必要工具、代码实现及注意事项。
在开始编码之前,我们需要了解几个核心概念并准备相应的开发环境:

sc0vu/web3php(基于 web3.js 的 PHP 移植)和 php-ethereum 等,本文将以 sc0vu/web3php 为例进行讲解。环境准备:
通过 Composer 安装 sc0vu/web3php 库:
composer require sc0vu/web3php
以下是使用 PHP 和 sc0vu/web3php 实现以太坊转账接口的详细步骤和代码示例:
require 'vendor/autoload.php'; use Web3\Web3; use Web3\Providers\HttpProvider; use Web3\RequestManagers\HttpRequestManager; // 以太坊节点 URL,Infura 的 Goerli 测试网络 URL $nodeUrl = 'https://goerli.infura.io/v3/YOUR_INFURA_PROJECT_ID'; // 创建 Provider $provider = new HttpProvider(new HttpRequestManager($nodeUrl, 5000)); // 5000 是超时时间(毫秒) // 创建 Web3 实例 $web3 = new Web3($provider);
我们需要以下信息来构建一笔交易:
$fromAddress = '0xYourSenderAddress'; // 发送方地址 $toAddress = '0xRecipientAddress'; // 接收方地址 $privateKey = 'YOUR_SENDER_PRIVATE_KEY'; // 发送方私钥(注意:实际应用中绝对不要硬编码,应从安全的地方获取) $amountInEth = '0.01'; // 转账的 ETH 数量 // 将 ETH 转换为 Wei $amountInWei = $web3->utils->toWei($amountInEth, 'ether');
Nonce 是一个账户发送的交易序号,用于防止重放攻击,必须获取当前账户的下一个可用 Nonce。
$nonce = null;
$web3->eth->getTransactionCount($fromAddress, 'latest', function ($err, $count) use (&$nonce) {
if ($err !== null) {
// 处理错误
echo "Error getting nonce: " . $err->getMessage();
return;
}
$nonce = $count;
echo "Nonce: " . $nonce . "\n";
});
// 注意:这里需要确保 nonce 已经获取到,在实际应用中可能需要使用异步或等待机制
// 为了简化示例,我们假设 nonce 能立即获取到,但在生产环境中需要更健壮的处理
为了避免因 Gas Limit 不足导致交易失败,可以先估计交易所需的 Gas Limit。
$gasLimit = null;
$web3->eth->estimateGas([
'from' => $fromAddress,
'to' => $toAddress,
'value' => $amountInWei
], function ($err, $gas) use (&$gasLimit) {
if ($err !== null) {
echo "Error estimating gas: " . $err->getMessage();
return;
}
$gasLimit = $gas;
echo "Estimated Gas Limit: " . $gasLimit . "\n";
});
// 同样,这里需要确保 gasLimit 已经获取到
获取到所有必要信息后,就可以构建交易、使用私钥签名,然后发送到以太坊网络。
// 假设我们已经获取到了 nonce, gasLimit
// 如果没有估计,可以设置一个合理的默认值,21000(简单转账的常见 Gas Limit)
if ($gasLimit === null) {
$gasLimit = '21000';
}
// 设置 gasPrice,20 Gwei
$gasPriceInGwei = '20';
$gasPriceInWei = $web3->utils->toWei($gasPriceInGwei, 'gwei');
$transaction = [
'from' => $fromAddress,
'to' => $toAddress,
'value' => $amountInWei,
'nonce' => $nonce,
'gas' => $gasLimit,
'gasPrice' => $gasPriceInWei,
];
// 使用私钥签名交易
$web3->eth->sendTransaction($transaction, $privateKey, function ($err, $transactionHash) {
if ($err !== null) {
echo "Error sending transaction: " . $err->getMessage();
return;
}
echo "Transaction sent! Hash: " . $transactionHash . "\n";
// 这里可以将 transactionHash 保存到数据库,方便后续查询交易状态
});
完整示例代码(简化版,需注意异步处理):
<?php
require 'vendor/autoload.php';
use Web3\Web3;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Utils;
class EthereumTransfer {
private $web3;
private $fromAddress;
private $privateKey;
public function __construct($nodeUrl, $fromAddress, $privateKey) {
$this->web3 = new Web3(new HttpProvider(new HttpRequestManager($nodeUrl, 5000)));
$this->fromAddress = $fromAddress;
$this->privateKey = $privateKey;
}
public function sendTransfer($toAddress, $amountInEth, $gasPriceGwei = '20', $gasLimit = '21000') {
try {
// 1. Convert ETH to Wei
$amountInWei = Utils::toWei($amountInEth, 'ether');
// 2. Get nonce
$nonce = $this->getNonce();
if ($nonce === null) {
throw new Exception("Failed to get nonce.");
}
// 3. Convert gas price to Wei
$gasPriceInWei = Utils::toWei($gasPriceGwei, 'gwei');
// 4. Prepare transaction
$transaction = [
'from' => $this->fromAddress,
'to' => $toAddress,
'value' => $amountInWei,
'nonce' => $nonce,
'gas' => $gasLimit,
'gasPrice'
本文由用户投稿上传,若侵权请提供版权资料并联系删除!