الخطة المجانية تغطي المشاريع الشخصية. الدفع حسب الاستخدام يتوسع دون بطاقة.
PHP
web3.php is the primary PHP library for talking to the Base JSON-RPC API, letting a Laravel or plain-PHP backend read balances, send transactions, and call contracts on the Coinbase-built L2. Since Base is EVM-equivalent with ETH gas, the standard eth methods work against https://base.therpc.io/YOUR_API_KEY at chain ID 8453. Install it with composer require web3p/web3.php. The library uses a callback-based API, so wrap your calls in a service class to keep that callback plumbing out of your controllers.
web3.php uses a callback-based API: each Base call hands its result to a closure rather than returning it directly. For Laravel projects, wrapping these calls in a dedicated service class keeps controllers clean and gives you one place to manage the Base connection.
Web3.php
Instantiate the client with new Web3(new HttpProvider(new HttpRequestManager('https://base.therpc.io/YOUR_API_KEY'))), pointing the request manager at your Base endpoint. Every eth method takes a function ($err, $result) callback — always check $err first and only use $result when it is null, so a failed Base request raises a clear exception instead of silently passing a null value downstream.
<?php
use Web3\Web3;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;
class EthereumService
{
private $web3;
public function __construct()
{
$this->web3= new Web3(new HttpProvider(new HttpRequestManager('https://base.therpc.io/YOUR_API_KEY')));
}
public function getBalance(string $address): string
{
$balance = null;
$this->web3->eth->getBalance($address, 'latest', function ($err, $result) use (&$balance) {
Key features: full Base JSON-RPC support, contract interactions, transaction handling, ETH unit conversion, and ABI encoding/decoding
Laravel Integration
In Laravel, read your Base endpoint from config('services.ethereum.node_url') — backed by a .env entry — so the URL and API key stay out of version control. Wrap repeated reads like balance lookups in Cache::remember to avoid hitting the Base node on every request; a short TTL is fine since balances change at most once per ~2s block, and it keeps your request quota under control.
<?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;
}
}
Smart Contract Integration
To work with a contract on Base, use the Contract class from web3.php, then bind it to the deployed address with ->at($contractAddress). Call read-only methods with ->call(...), passing a callback that receives the decoded result — the same callback-and-check-$err pattern used everywhere in web3.php. Verified ABIs and addresses for Base contracts are available on https://basescan.org.
<?php
use Web3\Contract;
class SmartContractService
{
private $contract;
public function __construct(string $abi, string $contractAddress)
{
$web3= new Web3(new HttpProvider(new HttpRequestManager('https://base.therpc.io/YOUR_API_KEY')));
$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;
}
}
Error Handling
Define a domain EthereumException that wraps the original Base error and carries it alongside a readable message. This gives your application structured catch blocks — you can handle a Base RPC failure distinctly from other application errors and still inspect the underlying cause when logging.
<?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