安装
通过Composer来管理依赖关系,首先将minimum-stability设置为dev
"minimum-stability": "dev"
然后执行:% m6 n; \/ c$ ]9 k& ]! N
composer require sc0vu/web3.php dev-master, d( ~. ~; u6 \8 U% D7 w
或者你可以在composer.json中添加这行。! A s2 U/ s& F0 o0 @4 _
"sc0vu/web3.php": "dev-master"# i0 y7 d4 ~$ }$ f- g0 W
用法, R8 e3 Z# B+ O: N( T
实例( w( d Z+ S, W' i
- use Web3\Web3;
- $web3 = new Web3('http://localhost:8545');
- 接口调用
- use Web3\Web3;& d8 T$ a% Y& ?. y" f, I$ q
- use Web3\Providers\HttpProvider;' x, f9 n! D& i% g3 V
- use Web3\RequestManagers\HttpRequestManager;5 E0 }7 w+ G7 P/ M3 p9 x
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));! E, }' Z: ?' |/ I5 a* P- i
- // timeout
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545', 0.1)));
使用回调函数调用rpc
- $web3->clientVersion(function ($err, $version) {8 ]# B) F" s! F
- if ($err !== null) {" N9 D% L, r0 M& a: E; S( `
- // do something( P5 T1 x8 u! V: H1 S/ V8 S5 U. F
- return;, h" t& l% J! R" ], F
- }8 o5 B' a5 ~3 r0 d' z2 q
- if (isset($client)) {; k& v S0 L* {. J9 m6 R2 D( _" d; v% a
- echo 'Client version: ' . $version;
- }
- });
- eth, S k5 z& f: ~) t3 Y, p7 V
- use Web3\Web3;) M# Q5 Q" c( H$ c. g
- $web3 = new Web3('http://localhost:8545');
- $eth = $web3->eth;3 Y6 q# L1 X$ q& K# f. ]
- 这样也行:
- use Web3\Eth;, P& R- N9 d# L7 l7 u$ P- d
- $eth = new Eth('http://localhost:8545');5 T" l. c+ O/ C
- net* V2 S) D. M, o% {2 G
- use Web3\Web3;
- $web3 = new Web3('http://localhost:8545');
- $net = $web3->net;
- 或者
- use Web3\Net;
- $net = new Net('http://localhost:8545');; e' u& ^) m6 B& f1 _, O4 Q6 R% k
- batch
- web31 H# i7 b9 G; A; s( S
- $web3->batch(true);0 q3 ]$ T9 ` A# } O5 X( Q
- $web3->clientVersion();0 n0 v; q& p8 I
- $web3->hash('0x1234');
- $web3->execute(function ($err, $data) {( ?* A: M; T2 d {/ p, v& h
- if ($err !== null) {: x2 O# r2 X( \/ G( i; _
- // do something4 `' H# G" Y' n
- // it may throw exception or array of exception depends on error type
- // connection error: throw exception
- // json rpc error: array of exception3 J' n4 X- r# c$ n- a: r$ T% v
- return;/ }' p. G1 v9 b9 L6 g$ D1 s* [8 l
- }
- // do something
- });. @* ?+ \, o* G! r
- eth3 X% ~2 j7 k3 Y$ z, {( D$ f3 m
- $eth->batch(true);
- $eth->protocolVersion();2 n, E5 I1 g+ t* F# k1 l# J
- $eth->syncing();# X5 N7 l- {7 R: i) f4 g/ n
- $eth->provider->execute(function ($err, $data) {& L2 d) \4 x" O6 N S# o
- if ($err !== null) {! q5 X" U2 b U& F
- // do something
- return;+ `& i, m" C1 @. ]2 c: \
- }
- // do something
- });
- net6 }! M% z0 o& t9 E1 `+ H+ Q+ `- p
- $net->batch(true);; w; m, [5 {' V1 }; Y
- $net->version();/ ?: K( X- h7 s
- $net->listening();. B s6 |2 e, F8 g
- $net->provider->execute(function ($err, $data) {
- if ($err !== null) {2 h; k5 Y5 d$ z+ z4 G+ t+ U4 @+ g
- // do something
- return;8 U& ?( p9 O% t! o
- }
- // do something9 w* r) E- K m; m
- });
- personal
- $personal->batch(true);
- $personal->listAccounts();% n% D' [; N* _; x
- $personal->newAccount('123456');
- $personal->provider->execute(function ($err, $data) {
- if ($err !== null) {" W h* Z; x* n8 `; `
- // do something& u2 h9 n9 ^7 e( s
- return;3 B" E: h: U' }) _: R8 s' u
- }! Q) p i. t1 n( @0 V4 |
- // do something
- }); P/ y) j+ ?/ S* {! a' }% J
- 智能合约Contract4 u1 _% d, X. `0 U+ n2 z
- use Web3\Contract;4 }7 K! K: R* p/ `- e
- $contract = new Contract('http://localhost:8545', $abi);
- // deploy contract9 P7 S) ^1 v! G
- $contract->bytecode($bytecode)->new($params, $callback);" y/ R& ]8 k7 S" ~3 ]( S9 y6 [) X+ W
- // call contract function# `, B* E- P7 I. |0 X( k
- $contract->at($contractAddress)->call($functionName, $params, $callback);
- // change function state
- $contract->at($contractAddress)->send($functionName, $params, $callback);" |/ k! u% Q2 F6 J; D" e
- // estimate deploy contract gas
- $contract->bytecode($bytecode)->estimateGas($params, $callback);
- // estimate function gas
- $contract->at($contractAddress)->estimateGas($functionName, $params, $callback);
- // get constructor data; [( L9 B% ]$ s8 M# A, y0 i
- $constructorData = $contract->bytecode($bytecode)->getData($params);
- // get function data
- $functionData = $contract->at($contractAddress)->getData($functionName, $params);
- 将值分配给外部域(从回调域到域外) N. e* d$ u4 r
- 由于和JavaScript回调不同,如果需要将值赋值到域外,我们需要给回调赋值。
- $newAccount = '';- W: g+ H7 f6 M O9 h. P7 G0 M& B1 S
- $web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {
- if ($err !== null) {
- echo 'Error: ' . $err->getMessage();2 V( }/ Y8 J" C) Q) X
- return;$ U7 z# B$ Q$ ?! ?
- }) {' w: B& z" o. r- T* Y
- $newAccount = $account;
- echo 'New account: ' . $account . PHP_EOL;
- });
php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。