php以太坊JSON-RPC API接口库

栏目: PHP · 发布时间: 7年前

内容简介:介绍一个php以太坊JSON-RPC API接口库,全面实现了web3可支持php7.x ——作者digitaldonkey,使用ethereum-php需要先添加库到用法:

介绍一个 php 以太坊JSON-RPC API接口库,全面实现了web3可支持php7.x —— ethereum-php

作者digitaldonkey,使用ethereum-php需要先添加库到 composer.json 文件:

{
  "minimum-stability":"dev",
  "autoload": {
    "psr-4": {
      "Ethereum\\": "src/"
    }
  },
  "repositories": [
    {
      "type": "git",
      "url": "https://github.com/digitaldonkey/ethereum-php.git"
    }
  ],
  "require": {
    "digitaldonkey/ethereum-php": "dev-master"
  }
}

用法:

composer require digitaldonkey/ethereum-php

这个库也是在 Drupal以太坊模块composer.json 中重要的组成部分。该模块旨在成为一组微型模块,提供各种方法来连接Drupal和以太坊区块链。

require __DIR__ . '/vendor/autoload.php';
use Ethereum\Ethereum;

try {
	// Connect to Ganache
    $eth = new Ethereum('http://127.0.0.1:7545');
    // Should return Int 63
    echo $eth->eth_protocolVersion()->val();
}
catch (\Exception $exception) {
    die ("Unable to connect.");
}

调用智能合约

你可以轻松地调用智能合约中的函数。

其中JSON文件“$fileName”是使用 Truffle 编译合约时所得到的。

$ContractMeta = json_decode(file_get_contents($fileName));
$contract = new SmartContract(
  $ContractMeta->abi,
  $ContractMeta->networks->{NETWORK_ID}->address,
  new Ethereum(SERVER_URL)
);
$someBytes = new EthBytes('34537ce3a455db6b')
$x = $contract->myContractMethod();
echo $x->val()

你还可以在智能合约中运行测试,check out EthTestClient

限制与结构

目前支持所有数据类型的数据,未来期望支持数组和列表。

这个库现在是只读的。这意味着你可以查询存储在以太坊区块链中的信息。

要写入区块链,需要用目前还不支持的私钥来签署交易。

php以太坊JSON-RPC API接口库

参考文档

可用的API文档:ethereum-php.org.

可以参考 以太坊RPC文档 和数据编码参考 Ethereum Wiki 中的 RLP dcumentation

或者去 以太坊前沿指南

  • php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。
  • C#以太坊,主要讲解如何使用C#开发基于.Net的以太坊应用,包括账户管理、状态与交易、智能合约开发与交互、过滤器和事件等。
  • web3j教程,主要是针对 java 和android程序员进行区块链以太坊开发的web3j详解。
  • 以太坊教程,主要介绍智能合约与dapp应用开发,适合入门。
  • 以太坊开发,主要是介绍使用node.js、 mongodb 、区块链、ipfs实现去中心化电商DApp实战,适合进阶。
  • python以太坊,主要是针对 python 工程师使用web3.py进行区块链以太坊开发的详解。

汇智网原创翻译,转载请标明出处。这里是 原文


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms Illuminated (Part 2)

Algorithms Illuminated (Part 2)

Tim Roughgarden / Soundlikeyourself Publishing, LLC / 2018-8-5 / USD 17.99

Algorithms are the heart and soul of computer science. Their applications range from network routing and computational genomics to public-key cryptography and machine learning. Studying algorithms can......一起来看看 《Algorithms Illuminated (Part 2)》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具