web3p/web3.php

Ethereum web3 接口。

0.3.2 2024-01-28 07:50 UTC

This package is auto-updated.

Last update: 2024-09-05 08:28:32 UTC


README

PHP Build Status codecov Join the chat at https://gitter.im/web3-php/web3.php Licensed under the MIT License

A php 接口,用于与以太坊区块链和生态系统交互。

安装

设置最小稳定性为 dev

"minimum-stability": "dev"

然后

composer require web3p/web3.php dev-master

或者在 composer.json 中添加此行

"web3p/web3.php": "dev-master"

用法

创建新实例

use Web3\Web3;

$web3 = new Web3('https://:8545');

使用提供者

use Web3\Web3;
use Web3\Providers\HttpProvider;

$web3 = new Web3(new HttpProvider('https://:8545'));

// timeout
$web3 = new Web3(new HttpProvider('https://:8545', 0.1));

您可以使用回调对每个 RPC 调用进行操作

$web3->clientVersion(function ($err, $version) {
    if ($err !== null) {
        // do something
        return;
    }
    if (isset($version)) {
        echo 'Client version: ' . $version;
    }
});

异步

use Web3\Web3;
use Web3\Providers\HttpAsyncProvider;

$web3 = new Web3(new HttpAsyncProvider('https://:8545'));

// timeout
$web3 = new Web3(new HttpAsyncProvider('https://:8545', 0.1));

// await
$promise = $web3->clientVersion(function ($err, $version) {
    // do somthing
});
Async\await($promise);

WebSocket

use Web3\Web3;
use Web3\Providers\WsProvider;

$web3 = new Web3(new WsProvider('ws://:8545'));

// timeout
$web3 = new Web3(new WsProvider('ws://:8545', 0.1));

// await
$promise = $web3->clientVersion(function ($err, $version) {
    // do somthing
});
Async\await($promise);

// close connection
$web3->provider->close();

Eth

use Web3\Web3;

$web3 = new Web3('https://:8545');
$eth = $web3->eth;

或者

use Web3\Eth;

$eth = new Eth('https://:8545');

Net

use Web3\Web3;

$web3 = new Web3('https://:8545');
$net = $web3->net;

或者

use Web3\Net;

$net = new Net('https://:8545');

批量

web3

$web3->batch(true);
$web3->clientVersion();
$web3->hash('0x1234');
$web3->execute(function ($err, $data) {
    if ($err !== null) {
        // do something
        // it may throw exception or array of exception depends on error type
        // connection error: throw exception
        // json rpc error: array of exception
        return;
    }
    // do something
});

eth

$eth->batch(true);
$eth->protocolVersion();
$eth->syncing();

$eth->provider->execute(function ($err, $data) {
    if ($err !== null) {
        // do something
        return;
    }
    // do something
});

net

$net->batch(true);
$net->version();
$net->listening();

$net->provider->execute(function ($err, $data) {
    if ($err !== null) {
        // do something
        return;
    }
    // do something
});

personal

$personal->batch(true);
$personal->listAccounts();
$personal->newAccount('123456');

$personal->provider->execute(function ($err, $data) {
    if ($err !== null) {
        // do something
        return;
    }
    // do something
});

Contract

use Web3\Contract;

$contract = new Contract('https://:8545', $abi);

// deploy contract
$contract->bytecode($bytecode)->new($params, $callback);

// call contract function
$contract->at($contractAddress)->call($functionName, $params, $callback);

// change function state
$contract->at($contractAddress)->send($functionName, $params, $callback);

// estimate deploy contract gas
$contract->bytecode($bytecode)->estimateGas($params, $callback);

// estimate function gas
$contract->at($contractAddress)->estimateGas($functionName, $params, $callback);

// get constructor data
$constructorData = $contract->bytecode($bytecode)->getData($params);

// get function data
$functionData = $contract->at($contractAddress)->getData($functionName, $params);

将值赋给外部作用域(从回调作用域到外部作用域)

由于回调不像 JavaScript 回调,如果我们需要将值赋给外部作用域,我们需要将引用赋给回调。

$newAccount = '';

$web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {
    if ($err !== null) {
        echo 'Error: ' . $err->getMessage();
        return;
    }
    $newAccount = $account;
    echo 'New account: ' . $account . PHP_EOL;
});

示例

要运行示例,您需要运行本地的以太坊区块链(testrpc)。

如果您使用 Docker 作为开发环境,可以尝试 ethdock 来运行本地的以太坊区块链,只需简单地运行 docker-compose up -d testrpc 并公开 8545 端口。

开发

已安装本地 php cli

  1. 克隆仓库并安装包。
git clone https://github.com/web3p/web3.php.git && cd web3.php && composer install
  1. 运行测试脚本。
vendor/bin/phpunit

容器

  1. 克隆仓库并运行容器。
git clone https://github.com/web3p/web3.php.git
  1. 将 web3.php 复制到 web3.php/docker/app 目录并启动容器。
cp files docker/app && docker-compose up -d php ganache
  1. 进入 php 容器并安装包。
docker-compose exec php ash
  1. TestCase.php 中更改 testHost
/**
 * testHost
 * 
 * @var string
 */
protected $testHost = 'http://ganache:8545';
  1. 运行测试脚本
vendor/bin/phpunit
安装包

首先进入容器

docker-compose exec php ash
  1. gmp
apk add gmp-dev
docker-php-ext-install gmp
  1. bcmath
docker-php-ext-install bcmath
移除扩展

将扩展配置从 /usr/local/etc/php/conf.d/ 移动

mv /usr/local/etc/php/conf.d/extension-config-name to/directory

API

待办事项。

贡献

感谢所有为 web3.php 做出贡献的人!

许可证

MIT