安装& @4 p2 k7 t$ b% y N2 M3 X
通过Composer来管理依赖关系,首先将minimum-stability设置为dev
"minimum-stability": "dev"
然后执行:/ I9 s) S6 a4 _4 j
composer require sc0vu/web3.php dev-master; `& H; i2 y5 M
或者你可以在composer.json中添加这行。
4 B, a3 H4 h. \1 n
"sc0vu/web3.php": "dev-master"4 r& O8 O/ B$ T Q, N7 S
用法
实例
- use Web3\Web3;9 X$ ~7 \* C3 W* D2 D o; \/ S0 _
- $web3 = new Web3('http://localhost:8545');
- 接口调用/ i! V( N0 L) m0 Y6 ^
- use Web3\Web3;
- use Web3\Providers\HttpProvider;
- use Web3\RequestManagers\HttpRequestManager;; e6 }3 e" K! ^- V* _# T' f. M4 M
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));
- // timeout
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545', 0.1)));
使用回调函数调用rpc1 S% f# D4 Y; M" z! d, T* y3 y
- $web3->clientVersion(function ($err, $version) {6 `+ v1 F: z t: j' }; j0 y/ q
- if ($err !== null) {! l5 w! A* L% a5 u! A. O; G* Z$ `
- // do something
- return;
- }! Y+ h" @ w, Y4 j( X+ N
- if (isset($client)) {
- echo 'Client version: ' . $version;
- }1 ]- U }2 h1 i2 T8 d; t, C
- });
- eth& u; C& y+ L+ O4 t8 U, r7 O2 h
- use Web3\Web3;
- $web3 = new Web3('http://localhost:8545');# H/ ]; f5 z! g8 [) O4 x! Y' o
- $eth = $web3->eth;
- 这样也行:
- use Web3\Eth;
- $eth = new Eth('http://localhost:8545');
- net7 A% ?2 p/ x) M0 L' t
- use Web3\Web3;; b; x& A4 `& v, P- A3 r
- $web3 = new Web3('http://localhost:8545');
- $net = $web3->net;
- 或者5 |. G% A3 v& [5 B/ s
- use Web3\Net;
- $net = new Net('http://localhost:8545');2 f7 t3 C% @" m. A
- batch
- web3
- $web3->batch(true);- I; {9 a0 B" A) y' `) N
- $web3->clientVersion();$ w+ j' ~6 v; p \9 ~6 J9 X% X
- $web3->hash('0x1234');
- $web3->execute(function ($err, $data) {
- if ($err !== null) {
- // do something9 C8 c- b' Y& h% g. L6 F
- // it may throw exception or array of exception depends on error type
- // connection error: throw exception$ A5 @6 `$ k; C
- // json rpc error: array of exception0 F) _, Q& ?, p3 c2 `4 O c
- return;
- }% r$ } D" P+ N2 h/ }) H# Q: V& r F
- // do something4 {1 H( }* k+ J
- });7 B$ W+ B8 \! _7 w. A& ~
- eth
- $eth->batch(true);- o1 g& m5 Z# R% R5 o6 v% @3 B, _
- $eth->protocolVersion();
- $eth->syncing();
- $eth->provider->execute(function ($err, $data) {
- if ($err !== null) {% b7 f$ `1 c2 C/ f0 H4 N A5 T. t- e
- // do something
- return;
- }* m$ L. B8 {( \6 W+ Z
- // do something; O5 a; O; k3 [4 P! Q! \
- });8 X* G, O( c" u4 B+ K! e, q# d. n. F
- net
- $net->batch(true);
- $net->version();
- $net->listening();
- $net->provider->execute(function ($err, $data) {1 j1 c/ Q& I" b9 D* C, f ~
- if ($err !== null) {& k, n3 L- z: b7 \& g! z
- // do something
- return;
- }
- // do something* \0 G$ v1 b) N: x8 r9 \- j
- });7 \; e( A; u% j# r" ]
- personal7 m2 ~* t) Q8 `# x$ I& b" ^. d0 s
- $personal->batch(true);
- $personal->listAccounts();8 }5 n9 W8 x% H8 J5 z5 r
- $personal->newAccount('123456');' i6 k: Z3 ]$ C9 r3 c6 }
- $personal->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something
- return;
- }1 P7 d9 M9 J3 j6 Q4 l2 h+ Q
- // do something5 m; [3 } u8 H# }2 E) ~
- });* w2 ~; w. T" p% _ W3 k
- 智能合约Contract& Z$ d4 g' e/ I- i
- use Web3\Contract;$ E# h* e- p/ i3 ]/ Z4 O
- $contract = new Contract('http://localhost:8545', $abi); i! g8 \2 \- r: [& S2 V
- // deploy contract
- $contract->bytecode($bytecode)->new($params, $callback);5 p* X- f! Z. O9 d/ c
- // call contract function( A/ W$ X, y" d0 m% [
- $contract->at($contractAddress)->call($functionName, $params, $callback);0 _1 @5 Q8 J0 U- n
- // change function state
- $contract->at($contractAddress)->send($functionName, $params, $callback);
- // estimate deploy contract gas' A& H6 T) Z. X$ X: |1 n
- $contract->bytecode($bytecode)->estimateGas($params, $callback);3 i7 b, T: J9 Q% C( Z+ Z" ]5 p
- // estimate function gas
- $contract->at($contractAddress)->estimateGas($functionName, $params, $callback);5 s) v2 D8 L: S: {# f/ u& d
- // get constructor data' b" E' K0 K0 g, G5 n
- $constructorData = $contract->bytecode($bytecode)->getData($params);
- // get function data
- $functionData = $contract->at($contractAddress)->getData($functionName, $params);& d* e: ?* i0 q. I' D/ }
- 将值分配给外部域(从回调域到域外)+ O x+ h" x* _. z
- 由于和JavaScript回调不同,如果需要将值赋值到域外,我们需要给回调赋值。, t" M$ c8 P9 ~) V+ ]0 s7 i. K( l9 u
- $newAccount = '';1 u! r( _' S' b* ^ }/ b( a
- $web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {
- if ($err !== null) {1 [; Y8 R5 f+ S* b$ S! S, L
- echo 'Error: ' . $err->getMessage();
- return;$ i% O, u6 E$ |: n5 c- T
- }: q" R$ p; v1 s# S
- $newAccount = $account;1 L: q7 a+ X" V8 g) w- ]1 t1 T
- echo 'New account: ' . $account . PHP_EOL;
- });
9 V# d2 H2 a: P' L7 j7 M
php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。