digitaldonkey/ethereum-php

一个整合以太坊与类型化PHP的库。

dev-master 2021-11-16 12:22 UTC

README

是PHP 7.1+的一个对Ethereum JSON-RPC API的类型化接口。

查看最新的API文档

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

这是composer.jsonDrupal Ethereum模块中的重要部分。

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()

您还可以在智能合约中运行测试,查看EthTestClient。

事件监听和处理

您可以使用Ethereum-PHP来监视智能合约的更改或逐块索引区块链。gs

参见UsingFiltersethereum-php-eventlistener

限制

目前不支持所有数据类型。

该库目前为只读。这意味着您可以检索存储在以太坊区块链中的信息。

要向区块链写入,需要使用私钥签名交易,但目前尚不支持。

architecture diagram

文档

API文档可在ethereum-php.org找到。

请参阅Ethereum RPC文档RLP文档,以及Ethereum Wiki中的数据编码。

还有一个更易读的Ethereum Frontier指南版本。