安装! g1 x# z4 l+ O0 [. ?
通过Composer来管理依赖关系,首先将minimum-stability设置为dev) ~! u# {' P- t* f
"minimum-stability": "dev"* M. m. [! E5 V0 d ?
然后执行:& }5 n& i% f% B5 X
composer require sc0vu/web3.php dev-master
或者你可以在composer.json中添加这行。, m- |* n F+ z# {: U
"sc0vu/web3.php": "dev-master"( |& V- m+ ~8 H( m9 U8 H/ ?
用法# x' p) v# q, ?* a- K
实例. l3 m' p$ l5 U9 \5 N$ L$ F3 Q
- use Web3\Web3;1 i; X0 ~! p \/ G6 d
- $web3 = new Web3('http://localhost:8545');
- 接口调用
- use Web3\Web3;
- use Web3\Providers\HttpProvider;9 f$ _# Q1 \" T( U% i: k
- use Web3\RequestManagers\HttpRequestManager;4 x. m% C" X5 \9 D
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));, I4 H$ U! e- G5 I4 H
- // timeout9 f F+ ?: U% {4 [0 I) ?
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545', 0.1)));
使用回调函数调用rpc
- $web3->clientVersion(function ($err, $version) {
- if ($err !== null) {9 g" S9 C: i: V: h! K6 o
- // do something
- return;
- }+ j+ Z0 \" {2 o- P4 x, ]# ]
- if (isset($client)) {
- echo 'Client version: ' . $version;
- }
- });
- eth# i) h1 k% a9 `) P& Q4 U. X( N
- use Web3\Web3;' e: m( V8 s5 T
- $web3 = new Web3('http://localhost:8545');
- $eth = $web3->eth;. N0 i* |4 d% }) d& [$ y
- 这样也行:
- use Web3\Eth;% c$ o- F% w& r9 A+ s/ q! A
- $eth = new Eth('http://localhost:8545');
- net; c# \( i ~5 b- n) i: r
- use Web3\Web3;. j+ \. R5 O2 z! m! f4 h
- $web3 = new Web3('http://localhost:8545');
- $net = $web3->net;: `7 y; `; P6 @" V [( ?8 u4 E4 z
- 或者
- use Web3\Net;
- $net = new Net('http://localhost:8545');
- batch3 x+ e. G6 ^, M4 @, W! Z5 {
- web39 Q+ Y! D3 f, K2 u
- $web3->batch(true);2 r$ ?7 l2 F; |, m
- $web3->clientVersion();! N! m8 V& \2 ?% `/ Y6 t
- $web3->hash('0x1234');
- $web3->execute(function ($err, $data) {
- if ($err !== null) {
- // do something
- // it may throw exception or array of exception depends on error type
- // connection error: throw exception5 H3 l$ a! |6 R4 N: x$ p: _
- // json rpc error: array of exception
- return;
- }' K0 I/ H9 F! `+ F Z* V' v
- // do something3 y9 o! M! x6 J I
- });% o: @6 n. S: I" r0 w' j1 L( x7 T4 A
- eth
- $eth->batch(true);+ @' k6 I/ o' V) _' L) C
- $eth->protocolVersion();
- $eth->syncing();& t! _2 c' i! R# U
- $eth->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something- V K- V% t0 q6 H# k
- return;
- }$ U( {, I0 _5 ~" ^1 \
- // do something
- });. s) m& n" R! a% f2 K3 e
- net: y5 ~0 P/ ], {* H: z) E
- $net->batch(true);- R8 a0 e0 a+ S" L% A( b d
- $net->version();( s9 V) P- |+ }; Z J) W- u
- $net->listening();
- $net->provider->execute(function ($err, $data) {- a7 V% _) ]; q
- if ($err !== null) {
- // do something3 [6 T8 C1 w w8 S* W
- return;% w, L3 w5 x+ Z9 b* d5 B
- }# I2 r/ ?6 ^- h( [
- // do something7 F# b! g: Q* z* [ t7 Z4 N
- });
- personal
- $personal->batch(true);' W+ ~6 f) j- v9 e# d3 v+ P) _
- $personal->listAccounts();' E9 L9 K) w, f4 m) P* s8 u
- $personal->newAccount('123456');
- $personal->provider->execute(function ($err, $data) {
- if ($err !== null) {, U& f. R% |) J( `" k: X
- // do something
- return;
- }
- // do something
- });. c6 P7 Z. }( G% b8 {
- 智能合约Contract6 G9 E8 u$ E7 F, b7 c3 m9 [7 n- ?8 P
- use Web3\Contract;$ v1 g# y) t5 D& ^+ V
- $contract = new Contract('http://localhost:8545', $abi);
- // deploy contract
- $contract->bytecode($bytecode)->new($params, $callback);
- // call contract function4 c* Z; B( Q/ u+ {' \. h. v" `
- $contract->at($contractAddress)->call($functionName, $params, $callback);: o- w! W( F' U3 H! s' i% O7 C
- // change function state
- $contract->at($contractAddress)->send($functionName, $params, $callback);2 |" |: P- E; _- i
- // estimate deploy contract gas
- $contract->bytecode($bytecode)->estimateGas($params, $callback);
- // estimate function gas3 l' i" J" q3 ?: F* S
- $contract->at($contractAddress)->estimateGas($functionName, $params, $callback);4 @1 u6 W$ ^9 {4 w. n
- // get constructor data( i( k+ Y0 v. r9 a
- $constructorData = $contract->bytecode($bytecode)->getData($params);+ ~% R% L3 K! B1 L- j( |
- // get function data. ?. {& q- w6 O. r+ ]' T5 T
- $functionData = $contract->at($contractAddress)->getData($functionName, $params);% @/ m% Y5 c. L1 X
- 将值分配给外部域(从回调域到域外); r8 m' V. Y/ Z* U
- 由于和JavaScript回调不同,如果需要将值赋值到域外,我们需要给回调赋值。
- $newAccount = '';
- $web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {
- if ($err !== null) {
- echo 'Error: ' . $err->getMessage();+ Q& e& e2 N0 {+ X
- return;& R1 P3 N- y9 p/ @' k( g
- }
- $newAccount = $account;, R( G& ^. `- J( ]) n) {# b
- echo 'New account: ' . $account . PHP_EOL;5 \* l; {% y- ^/ D7 e: a$ Q2 X
- });
php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。