Hi Guest

More contents, please log on!

Bitmere.com 区块链技术 Content
web3.php是一个PHP接口,主要用于与以太坊区块链及其生态系统进行交互.
安装
通过Composer来管理依赖关系,首先将minimum-stability设置为dev
"minimum-stability": "dev"
然后执行:
composer require sc0vu/web3.php dev-master
或者你可以在composer.json中添加这行。

"sc0vu/web3.php": "dev-master"
用法
实例
  1. use Web3\Web3;
  2. $web3 = new Web3('http://localhost:8545');
  3. 接口调用
  4. use Web3\Web3;
  5. use Web3\Providers\HttpProvider;
  6. use Web3\RequestManagers\HttpRequestManager;
  7. $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545')));
  8. // timeout
  9. $web3 = new Web3(new HttpProvider(new HttpRequestManager('http://localhost:8545', 0.1)));
Copy the Code

使用回调函数调用rpc
  1. $web3->clientVersion(function ($err, $version) {
  2.     if ($err !== null) {
  3.         // do something
  4.         return;
  5.     }
  6.     if (isset($client)) {
  7.         echo 'Client version: ' . $version;
  8.     }
  9. });
  10. eth
  11. use Web3\Web3;
  12. $web3 = new Web3('http://localhost:8545');
  13. $eth = $web3->eth;
  14. 这样也行:
  15. use Web3\Eth;
  16. $eth = new Eth('http://localhost:8545');
  17. net
  18. use Web3\Web3;
  19. $web3 = new Web3('http://localhost:8545');
  20. $net = $web3->net;
  21. 或者
  22. use Web3\Net;
  23. $net = new Net('http://localhost:8545');
  24. batch
  25. web3
  26. $web3->batch(true);
  27. $web3->clientVersion();
  28. $web3->hash('0x1234');
  29. $web3->execute(function ($err, $data) {
  30.     if ($err !== null) {
  31.         // do something
  32.         // it may throw exception or array of exception depends on error type
  33.         // connection error: throw exception
  34.         // json rpc error: array of exception
  35.         return;
  36.     }
  37.     // do something
  38. });
  39. eth
  40. $eth->batch(true);
  41. $eth->protocolVersion();
  42. $eth->syncing();
  43. $eth->provider->execute(function ($err, $data) {
  44.     if ($err !== null) {
  45.         // do something
  46.         return;
  47.     }
  48.     // do something
  49. });
  50. net
  51. $net->batch(true);
  52. $net->version();
  53. $net->listening();
  54. $net->provider->execute(function ($err, $data) {
  55.     if ($err !== null) {
  56.         // do something
  57.         return;
  58.     }
  59.     // do something
  60. });
  61. personal
  62. $personal->batch(true);
  63. $personal->listAccounts();
  64. $personal->newAccount('123456');
  65. $personal->provider->execute(function ($err, $data) {
  66.     if ($err !== null) {
  67.         // do something
  68.         return;
  69.     }
  70.     // do something
  71. });
  72. 智能合约Contract
  73. use Web3\Contract;
  74. $contract = new Contract('http://localhost:8545', $abi);
  75. // deploy contract
  76. $contract->bytecode($bytecode)->new($params, $callback);
  77. // call contract function
  78. $contract->at($contractAddress)->call($functionName, $params, $callback);
  79. // change function state
  80. $contract->at($contractAddress)->send($functionName, $params, $callback);
  81. // estimate deploy contract gas
  82. $contract->bytecode($bytecode)->estimateGas($params, $callback);
  83. // estimate function gas
  84. $contract->at($contractAddress)->estimateGas($functionName, $params, $callback);
  85. // get constructor data
  86. $constructorData = $contract->bytecode($bytecode)->getData($params);
  87. // get function data
  88. $functionData = $contract->at($contractAddress)->getData($functionName, $params);
  89. 将值分配给外部域(从回调域到域外)
  90. 由于和JavaScript回调不同,如果需要将值赋值到域外,我们需要给回调赋值。
  91. $newAccount = '';
  92. $web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {
  93.     if ($err !== null) {
  94.         echo 'Error: ' . $err->getMessage();
  95.         return;
  96.     }
  97.     $newAccount = $account;
  98.     echo 'New account: ' . $account . PHP_EOL;
  99. });
Copy the Code


php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。
BitMere.com is Information release platform,just provides information storage space services.
The opinions expressed are solely those of the author,Does not constitute advice, please treat with caution.
You have to log in before you can reply Login | 立即注册

Points Rules

Write the first review

白吃弟弟栋 小学生
  • Follow

    0

  • Following

    0

  • Articles

    4

币圈江左盟
Promoted