jackyxie/ethereum-php

一个集成以太坊与类型化PHP的库。感谢digitaldonkey

dev-master 2021-01-05 02:42 UTC

This package is auto-updated.

Last update: 2024-09-15 12:44:43 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指南版本。