安装: g0 v# O+ F4 f7 G" W
通过Composer来管理依赖关系,首先将minimum-stability设置为dev
"minimum-stability": "dev"/ a K) b- E$ [) B
然后执行:* M1 X' ]3 G! x" I- e
composer require sc0vu/web3.php dev-master
或者你可以在composer.json中添加这行。
" g1 Y2 m$ A. l" X) x! U2 b K9 ^0 O
"sc0vu/web3.php": "dev-master"
用法7 _" M+ D+ S: l) Z5 H, V; X6 P
实例
- use Web3\Web3;
- $web3 = new Web3('http://localhost:8545');$ |% j2 a) `' {+ |$ g
- 接口调用
- use Web3\Web3;- s9 j. }2 j; @+ G4 W8 i
- use Web3\Providers\HttpProvider;6 Z% S: L4 H, j$ A" D/ H( D
- use Web3\RequestManagers\HttpRequestManager;) o7 R7 e% z, F0 b" R1 ^% }
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));
- // timeout
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545', 0.1)));
使用回调函数调用rpc! Y6 N) F; P. U6 L4 t9 i
- $web3->clientVersion(function ($err, $version) {. l9 X+ M$ U, f0 Z$ ]7 `
- if ($err !== null) {: ^, F" N/ u5 P6 h* C
- // do something7 r6 k5 o$ P, w$ R
- return;
- }
- if (isset($client)) {/ v4 B: j9 g9 @, ?! A6 _: C6 a
- echo 'Client version: ' . $version;, M, `/ i( W! u( F4 E& u; k
- }' O, y# k! P' @" \) J6 e. t
- });' g& R( p, h- m; X! x J" j; l9 G
- eth
- use Web3\Web3;( H$ ]3 I$ x' ^! \3 D. J
- $web3 = new Web3('http://localhost:8545');
- $eth = $web3->eth;
- 这样也行:
- use Web3\Eth;
- $eth = new Eth('http://localhost:8545');+ u/ b% Y! p# ^8 M" C3 S
- net
- use Web3\Web3;
- $web3 = new Web3('http://localhost:8545');
- $net = $web3->net;
- 或者# l1 Y% c. L" v
- use Web3\Net;0 K4 }& T( l3 m }' S# B3 Q( I9 u
- $net = new Net('http://localhost:8545');: E# ]/ [/ K P9 Q* ~; U# T0 n
- batch: u: X" k$ u$ i
- web3" h& r2 m; }; I1 q1 l* d; M% v
- $web3->batch(true);
- $web3->clientVersion(); f& q+ W7 S# c U1 m
- $web3->hash('0x1234');
- $web3->execute(function ($err, $data) {
- if ($err !== null) {: X$ K0 v( k9 o# S
- // do something
- // it may throw exception or array of exception depends on error type k M* \9 ]; E9 y5 r6 K
- // connection error: throw exception+ l/ t( l. e% f7 M& l
- // json rpc error: array of exception
- return;; g7 E) G; R+ q9 T) i3 @3 ~
- }
- // do something. d- O7 o0 ?( S! F
- });8 g5 f C8 Y2 R
- eth
- $eth->batch(true);2 M; r' O) E3 Q* {6 f! X
- $eth->protocolVersion();# N$ \$ q& T% z2 q" p5 w: [4 f
- $eth->syncing();
- $eth->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something
- return;
- }
- // do something
- });7 x7 w. c0 z: V; v- R2 q s
- net6 o4 ^' b( s" B
- $net->batch(true);
- $net->version();/ \* w: q3 x1 Q1 g+ ^
- $net->listening();
- $net->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something
- return;
- }
- // do something7 E; E% a7 e( F% a+ r u9 q
- }); n& |9 @* z9 {+ {! c
- personal3 e* n: n! y! f. c% N: V
- $personal->batch(true);$ d$ q! {1 p! Z3 _7 t" h
- $personal->listAccounts();
- $personal->newAccount('123456');1 G6 `6 T4 l: Z$ ?
- $personal->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something3 h6 _/ k' g8 L( c8 `: Q
- return;
- }+ b% H. k6 _ z# c& w
- // do something
- });! W! M, u9 J3 a7 V0 ]9 m' v
- 智能合约Contract1 k h/ O- N4 s& u/ r& X0 J
- use Web3\Contract;
- $contract = new Contract('http://localhost:8545', $abi);
- // deploy contract2 ?. k* b! Y: l& i
- $contract->bytecode($bytecode)->new($params, $callback);
- // call contract function
- $contract->at($contractAddress)->call($functionName, $params, $callback);2 I5 l* a2 s0 s
- // change function state
- $contract->at($contractAddress)->send($functionName, $params, $callback);
- // estimate deploy contract gas8 _2 W3 c' t& N. ^
- $contract->bytecode($bytecode)->estimateGas($params, $callback);+ K; }, u" R& P9 J+ _
- // estimate function gas; g* A# U1 q, |) v
- $contract->at($contractAddress)->estimateGas($functionName, $params, $callback);
- // get constructor data5 Y7 {% O" p; Q4 ~3 E% m
- $constructorData = $contract->bytecode($bytecode)->getData($params);$ x' N% o B z8 E& [
- // get function data
- $functionData = $contract->at($contractAddress)->getData($functionName, $params);7 S$ b7 ?0 {" _/ D4 l( P
- 将值分配给外部域(从回调域到域外)
- 由于和JavaScript回调不同,如果需要将值赋值到域外,我们需要给回调赋值。
- $newAccount = '';
- $web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {
- if ($err !== null) {
- echo 'Error: ' . $err->getMessage();
- return;2 P+ Z) S" g ?" s- G; f
- }
- $newAccount = $account;& x1 A$ M5 @5 Y0 A
- echo 'New account: ' . $account . PHP_EOL;) l9 B' X. X7 C" F8 Z/ }* G
- });
) O8 p3 Q& ?. i
php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。