安装4 e) @" k$ n8 C
通过Composer来管理依赖关系,首先将minimum-stability设置为dev. j* C. x, I( t5 _
"minimum-stability": "dev"
然后执行:/ \% R/ y% n) L$ Q& T# M ^
composer require sc0vu/web3.php dev-master E, C) o% z1 f" w2 [
或者你可以在composer.json中添加这行。8 i* A+ Q; ^ @ i% A" |
"sc0vu/web3.php": "dev-master": v7 @+ N' I# @) F" B8 v
用法
实例
- use Web3\Web3;0 }' I$ ?9 H6 b0 M' a; f9 p% e: Z% W
- $web3 = new Web3('http://localhost:8545');& D6 ]7 f$ [5 p
- 接口调用1 m8 E: O9 S# d$ u* l
- use Web3\Web3;
- use Web3\Providers\HttpProvider;
- use Web3\RequestManagers\HttpRequestManager;
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));
- // timeout' K: y4 S8 n- W6 k
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545', 0.1)));
使用回调函数调用rpc
- $web3->clientVersion(function ($err, $version) {6 O0 a" l/ K6 A) p! c- [
- if ($err !== null) {9 w: ^( g1 S5 |4 Q% n' h/ D
- // do something; M. ]3 F7 W& t3 Z$ i B$ U
- return;2 [6 ~; D( L- o+ O; X2 \: V
- }) |9 p5 O9 C6 q6 x" [. n' T* \
- if (isset($client)) {4 s2 y$ o) ?$ |. ], P
- echo 'Client version: ' . $version;
- }2 g" ]; j! p* K2 O9 u8 f
- });$ n" f/ j H! \5 |! X: z3 O
- eth; @2 P9 d) P4 P6 y
- use Web3\Web3;
- $web3 = new Web3('http://localhost:8545');/ ]4 j. M! i- T9 B
- $eth = $web3->eth;
- 这样也行:
- use Web3\Eth;) A2 Q$ o6 X: X& A$ o( U
- $eth = new Eth('http://localhost:8545');
- net+ ]) X! ^4 O( D6 c' b, G( R
- use Web3\Web3;7 _7 y% I" _/ h' U; u% S
- $web3 = new Web3('http://localhost:8545');
- $net = $web3->net;* ~3 f) \8 S# n
- 或者
- use Web3\Net;/ |0 Z; {" p/ {; c
- $net = new Net('http://localhost:8545');
- batch
- web3
- $web3->batch(true);5 V* L% f7 ?9 ?" n+ f
- $web3->clientVersion();
- $web3->hash('0x1234');
- $web3->execute(function ($err, $data) {
- if ($err !== null) {; s. y; ^+ h& S2 _% K8 q1 B
- // do something k+ m4 I/ o) A0 S: X) K
- // it may throw exception or array of exception depends on error type; v- R8 q6 x5 C: E* l4 y$ K# G; Q
- // connection error: throw exception
- // json rpc error: array of exception% h& R+ m) k" x
- return;
- }- A9 |1 x& g. A; x' j X
- // do something
- });: \% l; h( `- L' a. }# ?4 k
- eth+ T# K( d8 K! ~ i. N
- $eth->batch(true);
- $eth->protocolVersion();( u8 k7 T4 {( m$ }* g# o
- $eth->syncing();
- $eth->provider->execute(function ($err, $data) {% \" E1 s9 \" j: t
- if ($err !== null) {0 Q8 K& ~6 N: M- p
- // do something: J$ P8 M0 @6 P/ c$ {; _$ Y8 \* }
- return;
- }8 [* h' Q- {) P' }. \
- // do something" O% h! p; K" L) @
- });
- net6 }7 p" i8 ~9 w4 |+ d. L* O2 `6 J
- $net->batch(true);
- $net->version();, q( m9 t _" z" F8 ?$ p8 q
- $net->listening();
- $net->provider->execute(function ($err, $data) {
- if ($err !== null) {3 l* |$ Q/ `% f3 d
- // do something
- return;
- }" r) l3 \$ J' @9 z( E
- // do something
- });6 Q- z; j0 [ {0 R8 X8 A
- personal9 ]& p7 {, G% G: H6 p
- $personal->batch(true);$ y. p' y, k- t1 d7 u
- $personal->listAccounts();3 A7 c( A2 o( w; W4 g- s; m# O
- $personal->newAccount('123456');* ?; Q/ }9 N% {% i* [
- $personal->provider->execute(function ($err, $data) {
- if ($err !== null) {* s) r3 G4 |) z& d! S
- // do something
- return;
- }
- // do something
- });% `: V& W3 r* r8 v
- 智能合约Contract
- use Web3\Contract;! X: U( x" I" A& m
- $contract = new Contract('http://localhost:8545', $abi);
- // deploy contract
- $contract->bytecode($bytecode)->new($params, $callback);
- // call contract function
- $contract->at($contractAddress)->call($functionName, $params, $callback);
- // change function state
- $contract->at($contractAddress)->send($functionName, $params, $callback);
- // estimate deploy contract gas
- $contract->bytecode($bytecode)->estimateGas($params, $callback);, @6 G! y1 W1 ~5 _1 |6 U
- // estimate function gas
- $contract->at($contractAddress)->estimateGas($functionName, $params, $callback);# ^2 \( e) A H4 f5 z% m+ K2 }
- // get constructor data+ ^ I* L3 }" ]: |2 y" ?
- $constructorData = $contract->bytecode($bytecode)->getData($params);( o, w" B9 p. g" Y
- // get function data
- $functionData = $contract->at($contractAddress)->getData($functionName, $params);
- 将值分配给外部域(从回调域到域外)& M- I8 u# V/ M7 d2 w$ N
- 由于和JavaScript回调不同,如果需要将值赋值到域外,我们需要给回调赋值。
- $newAccount = '';# u6 [) s3 F" c; `) q/ `- D6 ~& s# M
- $web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {( _3 z' I+ e2 y
- if ($err !== null) {7 m3 _' v) @$ |( Y5 l( [
- echo 'Error: ' . $err->getMessage();
- return;2 V+ m; J1 u( [9 l6 y. J
- }
- $newAccount = $account;
- echo 'New account: ' . $account . PHP_EOL;
- });
php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。