首先项目中安装Web3P,这是目前接入ETHRPC最全的PhP封装,几乎可以调用大部分Rpc接口
composer require sc0vu/web3.php dev-master
安装完成过后,在你的php文件中可以直接use,使用过程中请核对自己的命名空间
use Web3Web3; use Web3ProvidersHttpProvider; use Web3RequestManagersHttpRequestManager;
然后实例化web3对象,这里可以用infura也可以自建节点0.0.0.0:8545,博主用的infura作为演示
$web3 = new Web3(new HttpProvider(new HttpRequestManager('<#39;, 30)));
调用Eth函数,这里演示调用blockNumber函数,获取最新的区块号
function Test() { $web3 = new Web3(new HttpProvider(new HttpRequestManager('<#39;, 30))); $newblock=''; $web3->eth->blockNumber(function ($err, $data) use (&$newblock){ if(empty($err)) { $newblock = json_decode($data); } }); echo '最新的区块号:' .$newblock; } //返回值 最新的区块号:5540342
以上就是在PHP项目中最基础的接入ETH的方式,剩下大家就可以根据自己的业务逻辑去结合Eth了,是不是很简单?
关于web3.php里面的文件大家composer后可自行查阅web3封装了哪些方法
以上是Web3p安装完成后目录结构,好了大家今天就先分享这些吧,下期会给大家分享TRX接入方式,如果有不懂的可以给我留言我会一一解答的。
Woah! I’m really digging the template/theme
of this blog. It’s simple, yet effective. A
lot of times it’s difficult to get that “perfect balance”
between usability and visual appeal. I must say you have done a fantastic job with this.
In addition, the blog loads very quick for me on Internet explorer.
Excellent Blog!