gmicommunity/ethereum-php

一个将以太坊与类型化PHP集成的库。

该包的规范存储库似乎已消失,因此该包已被冻结。

dev-master 2022-01-27 16:46 UTC

This package is auto-updated.

Last update: 2022-12-27 18:58:18 UTC


README

数字驴/ethereum-php的分支,适用于现代PHP 8.1 / Laravel 8-9安装

原始文档

查看最新的API文档

composer.json文件中添加库

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

用法

composer require gmicommunity/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指南版本。