El plan gratuito cubre proyectos personales. El pago por uso escala sin necesidad de tarjeta.
PHP
web3.php is the primary PHP library for interacting with the Polygon JSON-RPC API. Since Polygon PoS is EVM-equivalent, its standard Ethereum method set works directly against https://polygon.therpc.io/YOUR_API_KEY. Install it with composer require web3p/web3.php. The library uses a callback-based API — every method takes a closure that receives an error and a result — so wrapping these calls in a dedicated service class keeps the callback noise out of your controllers and gives the rest of your app clean, synchronous-looking methods to call.
web3.php exposes a callback-based API, where each Polygon call hands its result to a closure. In Laravel projects, wrapping these calls in a service class keeps controllers clean and lets you bind the service into the container for reuse and testing.
Web3.php
Instantiate the client by chaining the providers: new Web3(new HttpProvider(new HttpRequestManager('https://polygon.therpc.io/YOUR_API_KEY'))). Every eth method takes a function ($err, $result) callback — always check $err first and only use $result when it is null, otherwise you risk reading a half-formed value from a failed Polygon request. The wei value that comes back can be converted to MATIC with the library's utils->fromWei(...) helper.
<?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://polygon.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 Polygon JSON-RPC support, contract interactions, transaction handling, wei-to-MATIC unit conversion, and ABI encoding/decoding
Laravel Integration
In Laravel, read the Polygon endpoint URL from config('services.ethereum.node_url') — backed by an environment variable — so your API key never lives in committed source. For frequently requested data like balances, wrap reads in Cache::remember with a short TTL; this collapses repeated lookups for the same address into a single Polygon RPC call and keeps your app responsive under load.
<?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 deployed Polygon contract, use the Contract class from web3.php: construct it with the provider and the contract ABI, then bind it to the on-chain address with ->at($contractAddress). Read methods are invoked with ->call($method, $params, $callback), where the callback receives the decoded result — following the same error-first convention as every other web3.php call.
<?php
use Web3\Contract;
class SmartContractService
{
private $contract;
public function __construct(string $abi, string $contractAddress)
{
$web3= new Web3(new HttpProvider(new HttpRequestManager('https://polygon.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-specific EthereumException that wraps the original error from a failed Polygon call. Throwing this typed exception instead of a bare Exception lets the rest of your app use structured catch blocks — distinguishing a Polygon RPC failure from any other error — and preserves the underlying cause for logging and debugging.
<?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