Hi Guest

More contents, please log on!

Bitmere.com 区块链技术 Content

PHP调用以太坊接口

西门幻雪雪vj
53 0 0
以太坊规定了每个节点需要实现的JSON RPC应用开发接口,如果希望使用PHP开发一个以太坊区块链上的去中心化应用(DApp),首先要解决的问题就是如何使用PHP调用以太坊JSON RPC接口:

如果你希望将网站快速接入以太坊,例如支持以太币支付,或者发行自己的代币,那么这个PHP以太坊开发详解课程提供了最佳的学习路径。

虽然接口规范并没有对传输层进行约定,但大多数以太坊节点软件都实现了对HTTP协议的支持。
因此我们可以使用Php的HTTP开发包来进行调用。例如,下面的代码使用guzzle这个http开发包来获取以太坊节点旳版本信息:

  1. [
  2.     'jsonrpc' => '2.0',
  3.     'method' => 'web3_clientVersion',
  4.     'params' => [],
  5.     'id' => time()
  6.   ]
  7. ];
  8. $rsp = $client->post('http://localhost:8545',$opts);
  9. echo $rsp->getBody() . PHP_EOL;
Copy the Code


当然你也可以借助于一些开源的封装,不过在php社区中,目前还缺乏得到统一认可的、相对比较成熟的以太坊开发包,因此在DApp的开发过程中,理解这些开源代码的实现原理就至关重要,而且往往需要综合利用多方面的代码资源才能解决问题。


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

西门幻雪雪vj 小学生
  • Follow

    0

  • Following

    0

  • Articles

    9

Promoted