PHPackages                             tourze/web3-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. tourze/web3-php

ActiveLibrary[API Development](/categories/api)

tourze/web3-php
===============

PHP library for interacting with Ethereum blockchain and Web3 services

1.0.1(6mo ago)0351MITPHPCI failing

Since Nov 3Pushed 5mo agoCompare

[ Source](https://github.com/tourze/web3-php)[ Packagist](https://packagist.org/packages/tourze/web3-php)[ RSS](/packages/tourze-web3-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (8)Versions (3)Used By (1)

Web3 PHP
========

[](#web3-php)

[English](README.md) | [中文](README.zh-CN.md)

A PHP library for interacting with Ethereum blockchain and Web3 services.

简介
--

[](#简介)

`tourze/web3-php` 是一个功能完整的 PHP 库，用于与以太坊区块链和 Web3 服务进行交互。它提供了简洁易用的 API，支持智能合约调用、交易发送、区块链数据查询等核心功能。

核心特性
----

[](#核心特性)

- 完整的 Web3 RPC 接口支持
- 智能合约 ABI 编解码
- 支持 ERC20、ERC721 等标准代币
- 支持 HTTP 和 WebSocket 连接
- 批量请求支持
- 完整的数据格式化和验证
- 详细的错误处理机制
- 类型安全的 PHP 8+ 支持

安装
--

[](#安装)

使用 Composer 安装：

```
composer require tourze/web3-php
```

快速开始
----

[](#快速开始)

### 基础连接

[](#基础连接)

```
use Tourze\Web3PHP\Web3;

// 连接到本地节点
$web3 = new Web3('http://localhost:8545');

// 连接到 Infura
$web3 = new Web3('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');
```

### 基本区块链操作

[](#基本区块链操作)

```
// 获取最新区块号
$web3->eth->blockNumber(function ($err, $blockNumber) {
    if ($err !== null) {
        echo 'Error: ' . $err->getMessage();
        return;
    }
    echo 'Block number: ' . $blockNumber . PHP_EOL;
});

// 获取账户余额
$web3->eth->getBalance('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb3', function ($err, $balance) {
    if ($err !== null) {
        echo 'Error: ' . $err->getMessage();
        return;
    }
    echo 'Balance: ' . $balance . PHP_EOL;
});
```

### 智能合约交互

[](#智能合约交互)

```
use Tourze\Web3PHP\Contract;

// ERC20 合约 ABI
$abi = json_decode(file_get_contents('erc20.abi.json'), true);

// 创建合约实例
$contract = new Contract($web3->provider, $abi);
$contract->at('0xCONTRACT_ADDRESS');

// 调用只读方法
$contract->call('balanceOf', '0xYOUR_ADDRESS', function ($err, $balance) {
    if ($err !== null) {
        echo 'Error: ' . $err->getMessage();
        return;
    }
    echo 'Token balance: ' . $balance[0] . PHP_EOL;
});

// 发送交易方法
$contract->send('transfer', '0xTO_ADDRESS', 1000000000000000000, [
    'from' => '0xYOUR_ADDRESS',
    'gas' => '0x76c0',
    'gasPrice' => '0x9184e72a000',
], function ($err, $transactionHash) {
    if ($err !== null) {
        echo 'Error: ' . $err->getMessage();
        return;
    }
    echo 'Transaction hash: ' . $transactionHash . PHP_EOL;
});
```

### 批量请求

[](#批量请求)

```
// 启用批量模式
$web3->batch(true);

// 添加多个请求
$web3->eth->blockNumber();
$web3->eth->getBalance('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb3');

// 执行批量请求
$web3->provider->execute(function ($err, $results) {
    if ($err !== null) {
        echo 'Error: ' . $err->getMessage();
        return;
    }
    echo 'Block number: ' . $results[0] . PHP_EOL;
    echo 'Balance: ' . $results[1] . PHP_EOL;
});
```

高级配置
----

[](#高级配置)

### 自定义 Provider

[](#自定义-provider)

```
use Tourze\Web3PHP\Providers\HttpProvider;
use Tourze\Web3PHP\RequestManagers\HttpRequestManager;

// 自定义请求管理器
$requestManager = new HttpRequestManager('http://localhost:8545', 30); // 30秒超时

// 自定义 Provider
$provider = new HttpProvider($requestManager);

// 使用自定义 Provider
$web3 = new Web3($provider);
```

### 工具函数

[](#工具函数)

```
use Tourze\Web3PHP\Utils;

// 转换单位: Ether 到 Wei
$wei = Utils::toWei('1', 'ether');
echo $wei; // 1000000000000000000

// 转换单位: Wei 到 Ether
$ether = Utils::fromWei('1000000000000000000', 'ether');
echo $ether; // 1

// 转换为十六进制
$hex = Utils::toHex('hello world');
echo $hex; // 0x68656c6c6f20776f726c64

// SHA3 哈希
$hash = Utils::sha3('hello world');
echo $hash;
```

### 地址验证

[](#地址验证)

```
use Tourze\Web3PHP\Utils;

// 验证地址格式
$isValid = Utils::isAddress('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb3');
echo $isValid ? 'Valid' : 'Invalid';

// 验证校验和地址
$isChecksumValid = Utils::isAddressChecksum('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb3');
echo $isChecksumValid ? 'Valid checksum' : 'Invalid checksum';
```

支持的方法
-----

[](#支持的方法)

### Web3 方法

[](#web3-方法)

- `web3_clientVersion`
- `web3_sha3`

### Net 方法

[](#net-方法)

- `net_version`
- `net_peerCount`
- `net_listening`

### Eth 方法

[](#eth-方法)

- `eth_protocolVersion`
- `eth_syncing`
- `eth_coinbase`
- `eth_mining`
- `eth_hashrate`
- `eth_gasPrice`
- `eth_accounts`
- `eth_blockNumber`
- `eth_getBalance`
- `eth_getStorageAt`
- `eth_getTransactionCount`
- `eth_getBlockTransactionCountByHash`
- `eth_getBlockTransactionCountByNumber`
- `eth_getUncleCountByBlockHash`
- `eth_getUncleCountByBlockNumber`
- `eth_getCode`
- `eth_sign`
- `eth_sendTransaction`
- `eth_sendRawTransaction`
- `eth_call`
- `eth_estimateGas`
- `eth_getBlockByHash`
- `eth_getBlockByNumber`
- `eth_getTransactionByHash`
- `eth_getTransactionByBlockHashAndIndex`
- `eth_getTransactionByBlockNumberAndIndex`
- `eth_getTransactionReceipt`
- `eth_getUncleByBlockHashAndIndex`
- `eth_getUncleByBlockNumberAndIndex`
- `eth_getCompilers`
- `eth_compileLLL`
- `eth_compileSolidity`
- `eth_compileSerpent`
- `eth_newFilter`
- `eth_newBlockFilter`
- `eth_newPendingTransactionFilter`
- `eth_uninstallFilter`
- `eth_getFilterChanges`
- `eth_getFilterLogs`
- `eth_getLogs`
- `eth_getWork`
- `eth_submitWork`
- `eth_submitHashrate`

### Personal 方法

[](#personal-方法)

- `personal_listAccounts`
- `personal_newAccount`
- `personal_unlockAccount`
- `personal_sendTransaction`

测试
--

[](#测试)

运行测试套件：

```
./vendor/bin/phpunit packages/web3-php/tests
```

质量保证
----

[](#质量保证)

- PHPStan Level 8 静态分析
- 测试覆盖率超过 90%
- 遵循 PSR-12 编码规范

贡献
--

[](#贡献)

欢迎提交 Issue 和 Pull Request！

许可证
---

[](#许可证)

MIT License

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance71

Regular maintenance activity

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Total

2

Last Release

185d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e354fdb316da535dfa8ba2e9193a473c403b6bc6fb9170778d1dc50e304c6e9d?d=identicon)[tourze](/maintainers/tourze)

---

Top Contributors

[![tourze](https://avatars.githubusercontent.com/u/13899502?v=4)](https://github.com/tourze "tourze (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tourze-web3-php/health.svg)

```
[![Health](https://phpackages.com/badges/tourze-web3-php/health.svg)](https://phpackages.com/packages/tourze-web3-php)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[saloonphp/saloon

Build beautiful API integrations and SDKs with Saloon

2.4k9.6M468](/packages/saloonphp-saloon)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[theodo-group/llphant

LLPhant is a library to help you build Generative AI applications.

1.5k311.5k5](/packages/theodo-group-llphant)[aporat/store-receipt-validator

PHP receipt validator for Apple App Store and Amazon Appstore

6503.9M9](/packages/aporat-store-receipt-validator)[googleads/googleads-php-lib

Google Ad Manager SOAP API Client Library for PHP

67410.3M25](/packages/googleads-googleads-php-lib)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
