web3-php / web3
Web3 PHP 是一个增强版的 PHP API 客户端,允许您与通用的以太坊 RPC 进行交互。
Requires
- php: ^8.0
- ext-bcmath: *
- guzzlehttp/guzzle: ^7.4.1
- phpseclib/phpseclib: ^3.0.13
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.6.0
- mockery/mockery: ^1.5.0
- pestphp/pest: ^2.0.0
- phpstan/phpstan: ^1.4.6
- symfony/var-dumper: ^5.4.3
This package is auto-updated.
Last update: 2023-02-11 23:40:37 UTC
README
Web3 PHP 是一个增强版的 PHP API 客户端,允许您与通用的以太坊 RPC 进行交互。
该项目正在开发中。代码和文档目前正在开发中,可能随时更改。
开始使用
需要 PHP 8.0+
首先,使用 Composer 软件包管理器安装 Web3
composer require web3-php/web3 dev-master
然后,与本地(web3-php/cli)或远程以太坊节点进行交互
use Web3\Web3; $web3 = new Web3('http://127.0.0.1:8545'); $accounts = $web3->eth()->accounts(); // ['0x54a3259f4f693e4c1e9daa54eb116a0701edc403', ...]
用法
Web3
命名空间
clientVersion
clientVersion
方法返回当前客户端的版本。
$web3->clientVersion(); // TestRPC v2.13.2
sha3
sha3
方法使用 Keccak-256 算法对数据进行哈希。
$web3->sha3('string'); // 0x348ab0847d053bb0150c1eb3470a71071d2967e20cf131b59dea3df9bf8f753e
Eth
命名空间
accounts
accounts
方法返回此客户端拥有的地址列表。
$web3->eth()->accounts(); // ['0x54a3259f4f693e4c1e9daa54eb116a0701edc403', ...]
chainId
chainId
方法返回当前链的 ID。
$web3->eth()->chainId(); // 1
gasPrice
gasPrice
方法返回当前气价的 wei。
$web3->eth()->gasPrice()->toEth(); // 0.00000002
getBalance
getBalance
方法返回地址的 wei 余额。
$web3->eth()->getBalance('0x54a3259f4f693e4c1e9daa54eb116a0701edc403')->toEth(); // 100
getBlockTransactionCountByHash
getBlockTransactionCountByHash
方法根据其哈希返回块中的交易数量。
$web3->eth()->getBlockTransactionCountByHash('0xd2a91777651a08b92d1d9fc701982c79da2249532cfe41a773a340978f96b5d1'); // 266
getTransactionByHash
getTransactionByHash
方法根据其哈希返回有关交易的信息。
$web3->eth()->getTransactionByHash('0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b');
getTransactionReceipt
getTransactionReceipt
方法根据其哈希返回交易的收据。
$web3->eth()->getTransactionReceipt('0xbb3a336e3f823ec18197f1e13ee875700f08f03e2cab75f0d0b118dabb44cba0');
getUncleCountByBlockHash
getUncleCountByBlockHash
方法根据其哈希返回块中的叔父数量。
$web3->eth()->getUncleCountByBlockHash('0xd2a91777651a08b92d1d9fc701982c79da2249532cfe41a773a340978f96b5d1'); // 266
hashrate
hashrate()
方法返回节点正在挖掘的每秒哈希数量。
$web3->eth()->hashrate(); // '65'
isMining
isMining()
方法确定客户端是否正在挖掘新块。
$web3->eth()->isMining(); // true
blockNumber
blockNumber()
方法返回客户端看到的最新块的编号(数量)。
$web3->eth()->blockNumber(); // '3220'
coinbase
coinbase()
方法返回客户端的Coinbase地址。
$web3->eth()->coinbase(); // '0xc014ba5ec014ba5ec014ba5ec014ba5ec014ba5e'
sendTransaction
sendTransaction
方法创建、签名并发送一个新的交易到网络。
use Web3\ValueObjects\{Transaction, Wei}; $from = '0xc9257b94da7f8eb07537db73a4ad0603cd83aba4'; $to = '0x108d1089e4a737c0be63527a6e464564be948b03'; $value = Wei::fromEth('1'); $transaction = Transaction::between($from, $to)->withValue($value); $web3->eth()->sendTransaction($transaction); // '0xa124a7de5177cf5cedd3c44e91d115d0011f915905fa36fb7c000a491fa536ee'
submitWork
submitWork()
方法提交工作量证明解决方案,并基于结果返回一个布尔值。
$web3->eth()->submitWork($nonce, $proofOfWorkHash, $mixDigest); // true
Net
命名空间
listening
listening
方法确定此客户端是否正在监听新的网络连接。
$web3->net()->listening(); // true
peerCount
peerCount
方法返回当前连接到此客户端的节点数。
$web3->net()->peerCount(); // 230
version
version
方法返回与当前网络关联的链ID。
$web3->net()->version(); // 1637712995212
Web3 PHP 是一个开源软件,受 MIT 许可 许可。