tourze / web3-php
PHP library for interacting with Ethereum blockchain and Web3 services
Installs: 27
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/tourze/web3-php
Requires
- guzzlehttp/guzzle: ^7.9.2
- guzzlehttp/psr7: ^2.6
- kornrunner/keccak: ^1.1
- phpseclib/phpseclib: ^3.0.43
- psr/http-message: ^1.1 || ^2.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- tourze/phpunit-base: 1.*
This package is auto-updated.
Last update: 2025-11-15 12:29:01 UTC
README
A PHP library for interacting with Ethereum blockchain and Web3 services.
简介
tourze/web3-php 是一个功能完整的 PHP 库,用于与以太坊区块链和 Web3 服务进行交互。它提供了简洁易用的 API,支持智能合约调用、交易发送、区块链数据查询等核心功能。
核心特性
- 完整的 Web3 RPC 接口支持
- 智能合约 ABI 编解码
- 支持 ERC20、ERC721 等标准代币
- 支持 HTTP 和 WebSocket 连接
- 批量请求支持
- 完整的数据格式化和验证
- 详细的错误处理机制
- 类型安全的 PHP 8+ 支持
安装
使用 Composer 安装:
composer require tourze/web3-php
快速开始
基础连接
use Tourze\Web3PHP\Web3; // 连接到本地节点 $web3 = new Web3('http://localhost:8545'); // 连接到 Infura $web3 = new Web3('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');
基本区块链操作
// 获取最新区块号 $web3->eth->blockNumber(function ($err, $blockNumber) { if ($err !== null) { echo 'Error: ' . $err->getMessage(); return; } echo 'Block number: ' . $blockNumber . PHP_EOL; }); // 获取账户余额 $web3->eth->getBalance('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb3', function ($err, $balance) { if ($err !== null) { echo 'Error: ' . $err->getMessage(); return; } echo 'Balance: ' . $balance . PHP_EOL; });
智能合约交互
use Tourze\Web3PHP\Contract; // ERC20 合约 ABI $abi = json_decode(file_get_contents('erc20.abi.json'), true); // 创建合约实例 $contract = new Contract($web3->provider, $abi); $contract->at('0xCONTRACT_ADDRESS'); // 调用只读方法 $contract->call('balanceOf', '0xYOUR_ADDRESS', function ($err, $balance) { if ($err !== null) { echo 'Error: ' . $err->getMessage(); return; } echo 'Token balance: ' . $balance[0] . PHP_EOL; }); // 发送交易方法 $contract->send('transfer', '0xTO_ADDRESS', 1000000000000000000, [ 'from' => '0xYOUR_ADDRESS', 'gas' => '0x76c0', 'gasPrice' => '0x9184e72a000', ], function ($err, $transactionHash) { if ($err !== null) { echo 'Error: ' . $err->getMessage(); return; } echo 'Transaction hash: ' . $transactionHash . PHP_EOL; });
批量请求
// 启用批量模式 $web3->batch(true); // 添加多个请求 $web3->eth->blockNumber(); $web3->eth->getBalance('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb3'); // 执行批量请求 $web3->provider->execute(function ($err, $results) { if ($err !== null) { echo 'Error: ' . $err->getMessage(); return; } echo 'Block number: ' . $results[0] . PHP_EOL; echo 'Balance: ' . $results[1] . PHP_EOL; });
高级配置
自定义 Provider
use Tourze\Web3PHP\Providers\HttpProvider; use Tourze\Web3PHP\RequestManagers\HttpRequestManager; // 自定义请求管理器 $requestManager = new HttpRequestManager('http://localhost:8545', 30); // 30秒超时 // 自定义 Provider $provider = new HttpProvider($requestManager); // 使用自定义 Provider $web3 = new Web3($provider);
工具函数
use Tourze\Web3PHP\Utils; // 转换单位: Ether 到 Wei $wei = Utils::toWei('1', 'ether'); echo $wei; // 1000000000000000000 // 转换单位: Wei 到 Ether $ether = Utils::fromWei('1000000000000000000', 'ether'); echo $ether; // 1 // 转换为十六进制 $hex = Utils::toHex('hello world'); echo $hex; // 0x68656c6c6f20776f726c64 // SHA3 哈希 $hash = Utils::sha3('hello world'); echo $hash;
地址验证
use Tourze\Web3PHP\Utils; // 验证地址格式 $isValid = Utils::isAddress('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb3'); echo $isValid ? 'Valid' : 'Invalid'; // 验证校验和地址 $isChecksumValid = Utils::isAddressChecksum('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb3'); echo $isChecksumValid ? 'Valid checksum' : 'Invalid checksum';
支持的方法
Web3 方法
web3_clientVersionweb3_sha3
Net 方法
net_versionnet_peerCountnet_listening
Eth 方法
eth_protocolVersioneth_syncingeth_coinbaseeth_miningeth_hashrateeth_gasPriceeth_accountseth_blockNumbereth_getBalanceeth_getStorageAteth_getTransactionCounteth_getBlockTransactionCountByHasheth_getBlockTransactionCountByNumbereth_getUncleCountByBlockHasheth_getUncleCountByBlockNumbereth_getCodeeth_signeth_sendTransactioneth_sendRawTransactioneth_calleth_estimateGaseth_getBlockByHasheth_getBlockByNumbereth_getTransactionByHasheth_getTransactionByBlockHashAndIndexeth_getTransactionByBlockNumberAndIndexeth_getTransactionReceipteth_getUncleByBlockHashAndIndexeth_getUncleByBlockNumberAndIndexeth_getCompilerseth_compileLLLeth_compileSolidityeth_compileSerpenteth_newFiltereth_newBlockFiltereth_newPendingTransactionFiltereth_uninstallFiltereth_getFilterChangeseth_getFilterLogseth_getLogseth_getWorketh_submitWorketh_submitHashrate
Personal 方法
personal_listAccountspersonal_newAccountpersonal_unlockAccountpersonal_sendTransaction
测试
运行测试套件:
./vendor/bin/phpunit packages/web3-php/tests
质量保证
- PHPStan Level 8 静态分析
- 测试覆盖率超过 90%
- 遵循 PSR-12 编码规范
贡献
欢迎提交 Issue 和 Pull Request!
许可证
MIT License