ihuangzhu/ethereum-php

一个将Ethereum与类型化PHP集成的库。

dev-master 2021-11-04 08:36 UTC

This package is auto-updated.

Last update: 2024-09-04 14:50:21 UTC


README

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

查看最新的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

局限性

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

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

写入区块链,您需要使用私钥签署交易,这目前尚不支持。

architecture diagram

文档

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

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

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