安装
通过Composer来管理依赖关系,首先将minimum-stability设置为dev
"minimum-stability": "dev"
然后执行:# U) r! _/ y/ F" J
composer require sc0vu/web3.php dev-master" ^# M9 {4 ^' _1 F) F
或者你可以在composer.json中添加这行。
7 ~& u: R* d) k2 M1 g* Y
"sc0vu/web3.php": "dev-master"
用法; d# l, m( i6 P. b2 N2 p
实例
- use Web3\Web3;% C3 b* ~5 S5 S
- $web3 = new Web3('http://localhost:8545');9 ~- E$ }) w& W: a
- 接口调用$ M# q" m8 i& P: T) t$ j4 b
- use Web3\Web3; p- U% q2 Q+ V8 d( ^
- use Web3\Providers\HttpProvider;% j$ J, ?% H2 |0 p, Q2 J
- use Web3\RequestManagers\HttpRequestManager;8 E1 g" n! o6 C) t* g+ C
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));7 f2 { Z4 M7 h4 y
- // timeout2 Z: v* ~8 w: m4 Z: {; N
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545', 0.1)));
使用回调函数调用rpc
- $web3->clientVersion(function ($err, $version) {% `1 i) n8 H9 m! z2 @ X
- if ($err !== null) {
- // do something) G, k8 }' }$ I- C
- return;5 s: Q0 n0 _2 W/ M4 t7 D
- }
- if (isset($client)) {2 |- Q ]+ Z4 k4 q
- echo 'Client version: ' . $version;
- }0 y6 a3 L0 J9 a* \0 n: p
- });3 n, }5 ]/ I" [" g) a
- eth3 _# l! A2 r" c
- use Web3\Web3;4 {- t& H0 ~1 B
- $web3 = new Web3('http://localhost:8545');
- $eth = $web3->eth;. P/ T. [6 I2 a4 K$ l% K. F
- 这样也行:
- use Web3\Eth;
- $eth = new Eth('http://localhost:8545');1 |. P5 N, L$ K
- net0 ]7 z. F8 O3 J& c2 F5 q
- use Web3\Web3;& C$ R/ ^. Q7 L4 A" S3 h" y& F# O" L
- $web3 = new Web3('http://localhost:8545');' M1 v9 d! G+ b( J+ C( z, j: B- g
- $net = $web3->net;
- 或者
- use Web3\Net;/ u0 V7 b+ K, i' `! Q
- $net = new Net('http://localhost:8545');
- batch' A3 {- q* X0 p
- web37 b7 e, ?' M1 N9 i/ k& u: ~
- $web3->batch(true);
- $web3->clientVersion();. f6 x5 l/ s; t) ?
- $web3->hash('0x1234');0 R" U" }; I) M1 }3 w' ]$ X
- $web3->execute(function ($err, $data) {
- if ($err !== null) {
- // do something. u7 q$ h3 I: V6 `2 s9 U+ @. n
- // it may throw exception or array of exception depends on error type
- // connection error: throw exception
- // json rpc error: array of exception
- return;$ [% e! k6 j- p& X$ e1 D& A/ W
- }
- // do something
- });
- eth
- $eth->batch(true);- R4 x+ z2 x' e, ?+ x7 c0 a
- $eth->protocolVersion();
- $eth->syncing();
- $eth->provider->execute(function ($err, $data) {
- if ($err !== null) {& X, ?+ P. q$ \% R5 b6 n" E
- // do something
- return;
- }- h6 O1 t: q3 n
- // do something
- });( d! ?1 [& B+ S' Y* X- o# E4 B4 D
- net
- $net->batch(true);5 _: \$ g- B5 k m, j+ E$ ~
- $net->version();
- $net->listening();0 V' a' t$ P6 Y$ L' M
- $net->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something, r5 ]6 W6 v4 u7 D7 x8 o1 D8 }3 L
- return;
- }3 K5 A. d: ^$ s' K; ]
- // do something0 l- x( E2 l: n6 F/ A
- });3 o5 G. W3 m: N" f
- personal
- $personal->batch(true);
- $personal->listAccounts();
- $personal->newAccount('123456');- |! w! N F- ^9 n$ ?' Z7 Y
- $personal->provider->execute(function ($err, $data) {6 u" T2 q5 g u% N# z
- if ($err !== null) {' v2 l' N7 Y% X3 k/ S. E# J
- // do something
- return;
- }
- // do something
- });- }7 ]1 z5 }4 T! e; g) u
- 智能合约Contract8 \" z+ t" O$ N0 a- ^. _0 A
- use Web3\Contract;6 m4 U1 W9 F4 [3 ~8 \9 Z2 X9 O& F
- $contract = new Contract('http://localhost:8545', $abi);5 \0 E7 n4 f! h% D
- // deploy contract
- $contract->bytecode($bytecode)->new($params, $callback);% A3 z4 r7 T2 m; o- L6 l
- // call contract function
- $contract->at($contractAddress)->call($functionName, $params, $callback);# _7 M0 Y: Q& a8 c. \2 I
- // change function state/ H+ V( P U2 U: j. B1 y/ T! ]4 b/ E
- $contract->at($contractAddress)->send($functionName, $params, $callback);
- // estimate deploy contract gas
- $contract->bytecode($bytecode)->estimateGas($params, $callback);" D+ U l! A6 k. f: X
- // estimate function gas
- $contract->at($contractAddress)->estimateGas($functionName, $params, $callback);
- // get constructor data* }+ `4 A& K$ f6 z
- $constructorData = $contract->bytecode($bytecode)->getData($params);7 C5 o! z2 \" P- |* @; n
- // get function data0 O& s+ v8 `3 T
- $functionData = $contract->at($contractAddress)->getData($functionName, $params);
- 将值分配给外部域(从回调域到域外)
- 由于和JavaScript回调不同,如果需要将值赋值到域外,我们需要给回调赋值。
- $newAccount = '';
- $web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {6 d4 R2 \' t. D( V w- A
- if ($err !== null) {
- echo 'Error: ' . $err->getMessage();
- return;
- }* m+ ?# y# ]8 X, V( G' f5 m" Z
- $newAccount = $account;. P y, G* e; s; H& l
- echo 'New account: ' . $account . PHP_EOL;, K6 w. d& H- w8 G
- });
" L( u# j/ g4 W m2 M
php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。