BNB Smart Chain
PHP
web3.php is the main library PHP developers use to talk JSON-RPC to BNB Smart Chain, letting a server-side app query balances, read BEP-20 token state, or submit transactions on chain 56 without shelling out to another runtime. Install it with composer require web3p/web3.php. One design quirk shapes how you use it: the API is callback-based rather than returning values directly, so the cleanest approach is to wrap those calls in a dedicated service class that exposes simple methods to the rest of your code.
Because every web3.php call hands its result to a closure instead of returning it, sprinkling those callbacks through controllers quickly becomes unreadable. In a Laravel project especially, funnel them through a single service class — the controller asks the service for a BSC balance and gets a clean value back, while the callback plumbing stays hidden behind that boundary.
Web3.php
A client nests three objects: new Web3(new HttpProvider(new HttpRequestManager("https://bsc.therpc.io/YOUR_API_KEY"))) gives you a handle to BNB Smart Chain over HTTP. From there, each eth method takes a function($err, $result) callback. Discipline matters here. Always inspect $err first and bail if it's set, because reading $result after a failed chain-56 request yields a null or partial value that will bite you downstream.
- GitHub: https://github.com/web3p/web3.php
- Packagist: https://packagist.org/packages/web3p/web3.php
- Full JSON-RPC support for BNB Smart Chain
- Contract interaction helpers for BEP-20 and custom Solidity
- Transaction construction and submission to chain 56
- BNB/wei unit conversion utilities
- ABI encoding and decoding for calldata and return values
Laravel Integration
In Laravel, keep the BSC endpoint out of your code by reading it from configuration — config("services.ethereum.node_url") resolves from an environment variable, so the URL with your API key never lands in version control. For read-heavy screens, wrap balance lookups in Cache::remember with a short TTL: repeated requests for the same address are served from the cache instead of hammering chain 56, which keeps response times low and your request volume sane.
Smart Contract Integration
For contract work, web3.php ships a Contract class you initialise with the ABI, then bind to a deployed address using ->at(), for example a BEP-20 token live on chain 56. A value read follows the same callback rhythm as the rest of the library: invoke ->call() with the function name and arguments, passing a closure that receives the decoded result so you can pull a token balance or symbol out of BNB Smart Chain.
Error Handling
Rather than passing raw callback $err values up the stack, define a domain EthereumException. Throw it from inside your service when a BSC call fails. Wrap the original error and you preserve the underlying detail while giving callers a single, intentional exception type to catch. A controller then writes one try/catch around its chain-56 interaction and responds consistently no matter where the failure originated.