PHP工具

PHP开发者可以使用Web3.php库进行以太坊开发,实现与Web应用程序的集成。

请注意!
嘿!友情提示:此页面旨在为您提供使用 PHP 请求的全面概述。有关实际代码示例,请查看我们的 API Methods documentation ,那里有所有支持语言的即用型示例!

# Web3.php

用于与以太坊节点交互的PHP接口。

<?php

use Web3\Web3;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;

class EthereumService
{
    private $web3;

    public function __construct(string $nodeUrl)
    {
        $this->web3 = new Web3(new HttpProvider(new HttpRequestManager($nodeUrl)));
    }

    public function getBalance(string $address): string
    {
        $balance = null;
        $this->web3->eth->getBalance($address, 'latest', function ($err, $result) use (&$balance) {
            if ($err !== null) {
                throw new Exception($err->getMessage());
            }
            $balance = $result;
        });

        return $this->web3->utils->fromWei($balance, 'ether');
    }

    public function sendTransaction(array $transaction): string
    {
        $hash = null;
        $this->web3->eth->sendTransaction($transaction, function ($err, $result) use (&$hash) {
            if ($err !== null) {
                throw new Exception($err->getMessage());
            }
            $hash = $result;
        });

        return $hash;
    }
}
  • GitHub: web3.php
  • Packagist: web3.php包
  • 特性:
    • JSON-RPC支持
    • 合约交互
    • 交易处理
    • ETH单位转换
    • ABI编码/解码

# Laravel集成

Laravel集成示例:

<?php

namespace App\Services;

use Web3\Web3;
use Illuminate\Support\Facades\Cache;

class EthereumService
{
    private $web3;

    public function __construct()
    {
        $this->web3 = new Web3(config('services.ethereum.node_url'));
    }

    public function getCachedBalance(string $address): string
    {
        return Cache::remember("eth_balance_{$address}", 300, function () use ($address) {
            return $this->getBalance($address);
        });
    }

    public function getTransactionCount(string $address): int
    {
        $count = null;
        $this->web3->eth->getTransactionCount($address, 'latest', function ($err, $result) use (&$count) {
            if ($err !== null) {
                throw new Exception($err->getMessage());
            }
            $count = hexdec($result);
        });

        return $count;
    }
}

# 智能合约集成

在PHP中使用智能合约:

<?php

use Web3\Contract;

class SmartContractService
{
    private $contract;

    public function __construct(string $nodeUrl, string $abi, string $contractAddress)
    {
        $web3 = new Web3(new HttpProvider(new HttpRequestManager($nodeUrl)));
        $this->contract = new Contract($web3->provider, $abi);
        $this->contract->at($contractAddress);
    }

    public function callMethod(string $method, array $params = [])
    {
        $result = null;
        $this->contract->call($method, $params, function ($err, $response) use (&$result) {
            if ($err !== null) {
                throw new Exception($err->getMessage());
            }
            $result = $response;
        });

        return $result;
    }
}

# 错误处理

PHP中的适当错误处理:

<?php

class EthereumException extends Exception
{
    private $ethError;

    public function __construct(string $message, $ethError = null)
    {
        parent::__construct($message);
        $this->ethError = $ethError;
    }

    public function getEthError()
    {
        return $this->ethError;
    }
}

class SafeEthereumService
{
    public function safeGetBalance(string $address): string
    {
        try {
            return $this->getBalance($address);
        } catch (Exception $e) {
            throw new EthereumException(
                "Failed to get balance for address: {$address}",
                $e
            );
        }
    }
}

另请参阅

帮助我们变得更好!
分享此页面并帮助我们为您创建更好的产品。