安装
通过Composer来管理依赖关系,首先将minimum-stability设置为dev+ B' j$ I& ?4 ]! \
"minimum-stability": "dev"2 z( f1 } l. V
然后执行:
composer require sc0vu/web3.php dev-master
或者你可以在composer.json中添加这行。5 P; y' j' J1 F
"sc0vu/web3.php": "dev-master"
用法5 j/ P; ?& w1 L# o
实例
- use Web3\Web3;5 y5 `! ~& M2 E
- $web3 = new Web3('http://localhost:8545');8 {' C( i1 _" l6 G" X
- 接口调用5 L, L: A6 f; W
- use Web3\Web3;
- use Web3\Providers\HttpProvider;
- use Web3\RequestManagers\HttpRequestManager;
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));4 g6 W! I& j# T# s# c: k) `% o- D
- // timeout0 T, R, z* {; X5 ~# B0 F
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545', 0.1)));
使用回调函数调用rpc9 s5 ^. u Y0 V% r* v5 Y) X
- $web3->clientVersion(function ($err, $version) {
- if ($err !== null) {
- // do something
- return;8 F, w6 T9 p; Q N2 G V
- }
- if (isset($client)) {
- echo 'Client version: ' . $version;8 }+ y- a( d& w
- }
- });
- eth
- use Web3\Web3;$ ~/ t3 b2 U9 z% w4 d+ ^
- $web3 = new Web3('http://localhost:8545');3 R; m% p$ R+ @" \% s3 Q- e
- $eth = $web3->eth;( p8 ] l6 D Q$ H/ P( W) P
- 这样也行:
- use Web3\Eth;. ~! A6 S, p: {9 ?' c, z! T6 i
- $eth = new Eth('http://localhost:8545');. I. B7 g6 F9 {( l+ Z% ]7 T, g* _" ]6 x+ q
- net
- use Web3\Web3;# x2 ^2 w9 G5 j6 T5 l1 K. @' C2 P
- $web3 = new Web3('http://localhost:8545'); P! E. L) B }- `5 n2 ?: D+ ~/ ~) Y
- $net = $web3->net;; j5 @! h: O3 G" A
- 或者
- use Web3\Net;
- $net = new Net('http://localhost:8545');/ u# u5 ?+ X5 R# @
- batch
- web3" E" K3 n7 c3 V+ i9 ?" _% }
- $web3->batch(true);
- $web3->clientVersion();; ?' {0 g( H/ E' n9 D8 ^
- $web3->hash('0x1234');
- $web3->execute(function ($err, $data) {
- if ($err !== null) {
- // do something+ M7 S' P$ |/ m" ~2 `# L1 M# S% O0 v
- // it may throw exception or array of exception depends on error type
- // connection error: throw exception9 T5 { u$ I9 ]" |2 F* s
- // json rpc error: array of exception' p. b. j: G' X! f! d
- return;( a/ p( \9 u; R; I1 |& L
- }( ]7 n, U3 |! {' Y) }
- // do something
- });
- eth
- $eth->batch(true);4 l1 Q0 a) q2 a. L
- $eth->protocolVersion();' @# i4 K+ I* x$ D4 l
- $eth->syncing();
- $eth->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something
- return;
- }) G( k' y' q+ ~
- // do something
- });" k, L( a; D9 \" [, m
- net
- $net->batch(true);
- $net->version();
- $net->listening();( f7 b+ B$ d, Y. Y# V' R5 s' D3 v
- $net->provider->execute(function ($err, $data) {$ k4 H7 Y9 j; |( Y5 x) e! T9 x
- if ($err !== null) {9 h* G. B) G% [8 f
- // do something3 S! v N3 M+ L7 ~6 f
- return;
- }% G/ P) W5 N: j2 F( r4 q
- // do something
- });
- personal( z7 c+ F. ~ B* Y9 T" M! m' s
- $personal->batch(true);" l4 {5 B# C6 S, L9 z2 A! m
- $personal->listAccounts();
- $personal->newAccount('123456');7 [& k: c, y8 b% E X* w2 K4 ~
- $personal->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something
- return;% c# |- M j# z+ W1 R6 |* ]
- }& {# ]. W. V+ z n9 B$ n
- // do something
- });
- 智能合约Contract- i$ s* H7 A7 s- ~ x% V) n' v- W* I0 e
- use Web3\Contract;
- $contract = new Contract('http://localhost:8545', $abi);
- // deploy contract
- $contract->bytecode($bytecode)->new($params, $callback);
- // call contract function: g& w1 ^8 T9 H& P: c: U, O
- $contract->at($contractAddress)->call($functionName, $params, $callback);
- // change function state0 t+ |& _9 j0 | z/ ?: M
- $contract->at($contractAddress)->send($functionName, $params, $callback);
- // estimate deploy contract gas
- $contract->bytecode($bytecode)->estimateGas($params, $callback);
- // estimate function gas2 d: n5 V$ Z0 P1 n2 B; N8 L4 h
- $contract->at($contractAddress)->estimateGas($functionName, $params, $callback); @, A# V G4 z% _0 A/ g! F+ c
- // get constructor data; d$ p' p/ M2 f/ F, _% o
- $constructorData = $contract->bytecode($bytecode)->getData($params);* R- i! C4 o: p1 G
- // get function data) u& M1 l2 z- i/ e0 o4 J! z8 T
- $functionData = $contract->at($contractAddress)->getData($functionName, $params);
- 将值分配给外部域(从回调域到域外)1 Q' B3 i, w" ^2 g
- 由于和JavaScript回调不同,如果需要将值赋值到域外,我们需要给回调赋值。# e3 n$ @! u! S
- $newAccount = '';
- $web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {8 o# y; c2 ~2 K# c7 N* w6 |
- if ($err !== null) {7 _0 ]# f) `8 J8 h9 n
- echo 'Error: ' . $err->getMessage();
- return;
- }
- $newAccount = $account;
- echo 'New account: ' . $account . PHP_EOL;0 U3 z& A0 P& p
- });
php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。