安装
通过Composer来管理依赖关系,首先将minimum-stability设置为dev
"minimum-stability": "dev"
然后执行:5 Y8 A1 m3 ]8 q3 n% v f& D
composer require sc0vu/web3.php dev-master2 k; C6 q: t* p/ W1 R1 w" n5 G, v
或者你可以在composer.json中添加这行。4 h7 h5 N8 O9 G+ V
2 y) |9 u8 P" H! c6 K" ], J
"sc0vu/web3.php": "dev-master"
用法
实例. O3 |8 V# {% Y K& l' T/ l1 }
- use Web3\Web3;7 ?( j3 |# ^5 Z$ a4 _
- $web3 = new Web3('http://localhost:8545');3 s6 u1 g# L3 p3 o2 S1 P
- 接口调用% R4 Q( J0 a0 L! H! I( }0 E
- use Web3\Web3;
- use Web3\Providers\HttpProvider;
- use Web3\RequestManagers\HttpRequestManager;
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));
- // timeout
- $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545', 0.1)));
使用回调函数调用rpc
- $web3->clientVersion(function ($err, $version) {4 |3 |" \' y6 x0 B9 |2 r! W
- if ($err !== null) {
- // do something+ W( \2 m7 a% n: [' _8 q0 F, I
- return;( [/ h4 ]! W9 Y; z" M. b! ~
- }" M8 l3 Z) o3 ]- V
- if (isset($client)) {
- echo 'Client version: ' . $version;
- }
- });( @* B; @/ Q# H) G, O
- eth
- use Web3\Web3;
- $web3 = new Web3('http://localhost:8545');
- $eth = $web3->eth;
- 这样也行:
- use Web3\Eth;4 I2 _& p, ?/ Z) ]
- $eth = new Eth('http://localhost:8545');
- net
- use Web3\Web3;- n: |2 M Q. U6 O) _) Z6 a
- $web3 = new Web3('http://localhost:8545');
- $net = $web3->net;+ L9 I' a* v, x/ I2 |& \2 t
- 或者
- use Web3\Net;
- $net = new Net('http://localhost:8545');- y+ Q+ Y3 K, r: Y4 A; w
- batch
- web33 a* ?/ D/ k, D+ n+ S1 m
- $web3->batch(true);
- $web3->clientVersion();1 s0 s" N* F: H2 y6 m
- $web3->hash('0x1234');! {# y3 a' A# u; X! E
- $web3->execute(function ($err, $data) {. j6 P9 `# B, O- Z6 `
- if ($err !== null) {; S- V3 ?4 C, ~1 A4 m) H6 v, g6 g
- // do something
- // it may throw exception or array of exception depends on error type; M3 B& N1 t' O: `4 y
- // connection error: throw exception
- // json rpc error: array of exception
- return;
- }; v d6 @% s1 P$ j0 V6 F. X
- // do something9 s% Q) s6 n( e! u
- });
- eth/ ^4 B6 p# I8 ^# ^, p1 \
- $eth->batch(true);! N6 f3 f' b4 g r5 H& v \
- $eth->protocolVersion();% [- ^5 V* m' |
- $eth->syncing();
- $eth->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something
- return;& D1 V0 e& [# T/ f
- }2 N1 s! f9 Q; N, W7 w% D ^
- // do something
- });& |2 t9 g/ `( |6 [
- net! T) E K. g/ U2 e) M5 ]. k V
- $net->batch(true);3 f/ J! x* ? P1 X
- $net->version();( i2 i, G5 N: Q( f" m% K/ j
- $net->listening();
- $net->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something
- return;
- }8 Y% O8 g' v3 z I% G% u9 K
- // do something! l! F1 b" B. {: j( s" S$ N+ U
- });
- personal
- $personal->batch(true);; C# V6 b% A9 K# b& T) f; O
- $personal->listAccounts();; L8 A, g9 ^7 Z$ i2 t! }
- $personal->newAccount('123456');/ F* h* k% ^" W4 o. a+ q+ a
- $personal->provider->execute(function ($err, $data) {
- if ($err !== null) {
- // do something
- return;& W4 O7 I u* o4 B& b4 g3 e9 p: G
- }# P/ y6 U. U" a1 E
- // do something
- });
- 智能合约Contract
- use Web3\Contract;
- $contract = new Contract('http://localhost:8545', $abi);
- // deploy contract0 Y5 ]" V# e: y
- $contract->bytecode($bytecode)->new($params, $callback);: v4 Q2 F) |3 I5 f% K+ t
- // call contract function) r8 u# k, {, N( f" I
- $contract->at($contractAddress)->call($functionName, $params, $callback);
- // change function state
- $contract->at($contractAddress)->send($functionName, $params, $callback);
- // estimate deploy contract gas+ Q* N( w+ {0 B9 ?5 f; l
- $contract->bytecode($bytecode)->estimateGas($params, $callback);& x, |9 T! o2 C! e. V
- // estimate function gas' b. E5 V. C) Z9 R9 J* f
- $contract->at($contractAddress)->estimateGas($functionName, $params, $callback);
- // get constructor data8 _) W! v6 X! W! O8 e
- $constructorData = $contract->bytecode($bytecode)->getData($params);: ^* n) V' Y: ^, h" N- E
- // get function data
- $functionData = $contract->at($contractAddress)->getData($functionName, $params);
- 将值分配给外部域(从回调域到域外)# }& G5 t4 V( z- ?
- 由于和JavaScript回调不同,如果需要将值赋值到域外,我们需要给回调赋值。
- $newAccount = '';
- $web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {
- if ($err !== null) {
- echo 'Error: ' . $err->getMessage();
- return;
- }
- $newAccount = $account;, i! X, a8 h2 K- z6 |7 }' ~
- echo 'New account: ' . $account . PHP_EOL;# \/ P: N% n/ N5 A) u8 O* {
- });
php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。