安装
通过Composer来管理依赖关系,首先将minimum-stability设置为dev
"minimum-stability": "dev"
然后执行: I W$ s2 v! l5 K& k
composer require sc0vu/web3.php dev-master! t8 q) j; S- O8 i+ g1 C
或者你可以在composer.json中添加这行。; p2 g, b/ ?' s; D2 j
"sc0vu/web3.php": "dev-master"0 q% h9 x$ n# u2 z
用法
实例
- use Web3\Web3;8 v% T' e0 k a, c- @! V$ P% g
- $web3 = new Web3('http://localhost:8545');2 v* [+ e1 R: [+ a
- 接口调用( c4 h# Y5 V$ N
- use Web3\Web3;' d1 M3 n5 Y9 Y/ Y
- use Web3\Providers\HttpProvider;+ I" n9 \' C1 e9 f4 r- G
- use Web3\RequestManagers\HttpRequestManager;) V9 t1 Q# V5 l- ], c
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));
- // timeout
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545', 0.1)));
使用回调函数调用rpc
- $web3->clientVersion(function ($err, $version) {
- if ($err !== null) {
- // do something. m; ^7 _( g" l
- return;
- }
- if (isset($client)) {
- echo 'Client version: ' . $version;- Y& x3 r6 @0 V; p; }6 l. I
- }
- });, G8 |# R, w1 k
- eth
- use Web3\Web3;$ X' C- e, o0 ~6 o, T& m
- $web3 = new Web3('http://localhost:8545');
- $eth = $web3->eth;
- 这样也行:
- use Web3\Eth;% i- T/ c' {* `- w
- $eth = new Eth('http://localhost:8545');' Y7 k2 @& \+ `3 O6 [/ R
- net
- use Web3\Web3;
- $web3 = new Web3('http://localhost:8545');
- $net = $web3->net;
- 或者
- use Web3\Net;7 {( e* [8 n) _. E0 p* m. N, Q
- $net = new Net('http://localhost:8545');3 Y3 O" p q* k0 m/ e8 @+ g& o/ m
- batch: `! N: W! }3 t& N
- web3
- $web3->batch(true);
- $web3->clientVersion();# f* g/ C0 I2 ^- m. ~5 t
- $web3->hash('0x1234');( k6 R$ w# o! Y2 k
- $web3->execute(function ($err, $data) {! C! N6 H/ v& ^5 c% u# b) o
- if ($err !== null) {
- // do something
- // it may throw exception or array of exception depends on error type
- // connection error: throw exception% i3 M" {, `; g1 ^2 N/ b8 Y7 P. Z
- // json rpc error: array of exception0 E3 }& n- U; U0 Q- j4 x: i5 y
- return;
- }$ J# p( ]9 ^$ V4 i1 [4 G
- // do something
- });
- eth
- $eth->batch(true);5 X! x9 R7 C1 m" ?2 M
- $eth->protocolVersion();6 t1 D4 ?5 b! @8 R6 n) u
- $eth->syncing();( W/ R, U3 h7 f& T4 |" H
- $eth->provider->execute(function ($err, $data) {
- if ($err !== null) {6 p; F4 Z4 H3 |: M4 y
- // do something
- return;
- }% L/ ]+ m( K2 a$ ~' E' [
- // do something8 I( f9 T- D$ c9 W9 ` ^9 U
- });
- net
- $net->batch(true);* ^- Z& D+ [# `4 j
- $net->version();
- $net->listening();# \, h( R- m X4 X. a
- $net->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something
- return;' A$ W* U3 z. X( `8 ?
- }
- // do something* k; e; u! K. w6 T
- });
- personal
- $personal->batch(true);/ |8 Q/ c# H0 @2 V' f- @: \
- $personal->listAccounts();
- $personal->newAccount('123456');; @; S0 o2 E0 T" l. [
- $personal->provider->execute(function ($err, $data) {
- if ($err !== null) {& a& t: r, n4 j9 b% Y
- // do something) z0 _* @; J; D# A* P
- return;* ]2 {; ~- k. K' n
- }2 v! i3 F# c/ G9 j/ S+ Z* U
- // do something
- });$ [/ y- `/ f6 q
- 智能合约Contract' _" [! j: _# j& @1 m
- use Web3\Contract;3 G w( O. _" I% A5 V( t6 l6 y
- $contract = new Contract('http://localhost:8545', $abi);0 r* ?) A; ?' W3 S/ i( @2 B) F
- // deploy contract# r) D% d4 g/ ^, V$ }& V. q8 U
- $contract->bytecode($bytecode)->new($params, $callback);) ]# [( H- U/ N3 p% L$ c t7 `
- // call contract function' g' @$ z5 p' \0 R$ ~
- $contract->at($contractAddress)->call($functionName, $params, $callback);
- // change function state% _: i$ q5 N2 d: U1 l8 F; B, m
- $contract->at($contractAddress)->send($functionName, $params, $callback);
- // estimate deploy contract gas
- $contract->bytecode($bytecode)->estimateGas($params, $callback);
- // estimate function gas R$ ]* w. d: \6 {" M# z
- $contract->at($contractAddress)->estimateGas($functionName, $params, $callback);
- // get constructor data# V, w5 P* U5 H# N
- $constructorData = $contract->bytecode($bytecode)->getData($params);) t* s( s7 `% z" n. N* l1 {
- // get function data
- $functionData = $contract->at($contractAddress)->getData($functionName, $params);5 r/ m/ r) B+ d0 q
- 将值分配给外部域(从回调域到域外)
- 由于和JavaScript回调不同,如果需要将值赋值到域外,我们需要给回调赋值。5 L- `( F, z4 m8 E% d2 f% W. t2 c
- $newAccount = '';- }6 B4 O' I; N; q& s: g9 `" A
- $web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {
- if ($err !== null) {
- echo 'Error: ' . $err->getMessage();8 j0 E; _1 G5 c% l, _& ~
- return;) H9 Z7 Z* ?" V5 t4 v1 \
- }7 x- E8 I% K) B+ ?% L' N& ]1 B! S& r
- $newAccount = $account;3 L' k. Q% x6 L
- echo 'New account: ' . $account . PHP_EOL;
- });
1 G& B4 Z+ J. Q/ E7 `. X5 T
php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。