PHP code example of jeavenguitar / web3php
1. Go to this page and download the library: Download jeavenguitar/web3php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jeavenguitar / web3php example snippets
$web3 = new Web3php\Web3(['https://bsc-dataseed.bnbchain.org']);
$usdt = $web3->contract($abiJson)->at('0x55d398326f99059fF775485246999027B3197955');
$usdt->call('name'); // "Tether USD"(ABI 解码,直接拿到标量)
$usdt->call('balanceOf', $userAddress); // "1234500000000000000"(uint256 → 十进制字符串,不丢精度)
$txHash = $usdt->send('transfer', [$to, '1000000'], $wallet); // 本地签名 + 自动补费 + 广播
use Web3php\Web3;
$web3 = new Web3([
'https://bsc-dataseed.bnbchain.org',
'https://bsc-dataseed1.defibit.io',
], [
'timeout' => 60, // 单次请求总超时(秒)
'connect_timeout' => 15, // 建连超时(秒)
'retries' => 3, // 同节点最大尝试次数
'retry_delay_ms' => 200, // 重试退避基数(第 n 次等 n × 200ms)
'failover' => true, // 多节点自动切换
// 'failover_error_codes' => [-32602], // 个别节点查不到的数据换节点再查(如久远回执)
// 'headers' => ['Authorization' => 'Bearer ...'], // 节点鉴权头
// 'verify_ssl' => true,
]);
$eth = $web3->eth();
$eth->blockNumber(); // int
$eth->chainId(); // int(BSC=56)
$eth->gasPrice(); // "5000000000"(wei 十进制字符串)
$eth->getBalance($address); // 原生币余额(wei 字符串)
$eth->getTransactionCount($address); // int nonce
$eth->getTransactionReceipt($hash); // ?object(未上链返回 null,不是错误)
$eth->getBlockByNumber('latest', false); // 区块
$eth->getLogs(['address' => $token, 'fromBlock' => $from]); // 事件日志
$receipt = $eth->waitForReceipt($hash, 60.0); // 轮询等回执
Eth::isSuccessful($receipt); // status == 0x1
$decimals = $token->call('decimals');
$display = bcdiv($token->call('balanceOf', $user), bcpow('10', $decimals), 6);
$abi = file_get_contents('ERC20.json'); // remix/hardhat 导出的 ABI 直接用
$token = $web3->contract($abi)->at($contractAddress);
// 单输出 → 直接是标量
$token->call('name'); // string
$token->call('balanceOf', $user); // uint256 → 十进制字符串
$token->call('decimals'); // uint8 → "18"
// 多输出 → 全部有名字时只返回字段名键(与链上一致,没有 0/1/2 混在里面)
$r = $pair->call('getReserves');
$r['reserve0']; // 直接按字段名取
// 有未命名输出时回退数字键,命名的照样给名字键
// struct / tuple 数组:每个元素是「字段名 => 值」数组,长度 = 元素个数
$infos = $nft->call('getGlobalNFTs', 0, 50); // 50 个元素的数组,不会按 word 数误截
// 指定区块 / 模拟 from
$token->callAt('balanceOf', [$user], ['block' => 19000000, 'from' => $other]);
// 只编码不解码(自己组 eth_call / 监听 methodId)
$calldata = $token->encode('transfer', [$to, '1000000']);
$values = $token->decode('getReserves', $hexFromRpc);
$wallet = $web3->wallet($privateKey); // 或 Web3php\Wallet::create() 新建
// 裸转账
$hash = $web3->sendTransaction($wallet, [
'to' => $payee,
'value' => '1000000000000000000', // 1 BNB(wei,字符串)
]);
// 合约方法调用:nonce / gas / gasPrice / chainId 全自动
$hash = $token->send('transfer', [$to, '1000000'], $wallet);
// 手动覆盖费用 / 切 EIP-1559
$hash = $token->send('transfer', [$to, '1000000'], $wallet, [
'gas' => '65000',
'maxFeePerGas' => '30000000000', // 给了它就是 type-2 交易
'maxPriorityFeePerGas' => '1000000000',
]);
$receipt = $web3->eth()->waitForReceipt($hash);
$results = $web3->rpc()->batch([
['method' => 'eth_getBalance', 'params' => [$a, 'latest']],
['method' => 'eth_getBalance', 'params' => [$b, 'latest']],
]);
// 结果与入参同序;单条失败不拖垮整批,对应项是 RpcException 实例
src/
├── Web3.php 门面入口
├── Eth.php eth_* / net_* API(值已归一化)
├── Wallet.php 私钥 → EIP-55 地址、签名
├── Contract/Contract.php 合约调用(call/encode/decode/estimateGas/send)
├── Rpc/RpcClient.php 稳健 JSON-RPC(重试/故障转移/keep-alive/批处理)
├── Abi/ ABI 解析与编解码(ParamType/Abi/Encoder/Decoder)
├── Tx/ RLP / 签名器(EIP-155、EIP-1559)/ 交易构建
├── Utils/ Hex / Address(EIP-55) / Crypto(Keccak)
└── Exception/ 异常体系