The bsc-php library provides developers with powerful tools to interact with the Binance Smart Chain (BSC) network. This PHP package enables seamless management of BNB and BEP20 digital assets, offering functionalities such as address creation, balance queries, transaction transfers, and comprehensive blockchain data retrieval.
Core Features and Capabilities
This library serves as a bridge between your PHP applications and the Binance Smart Chain ecosystem. It supports both mainnet and testnet environments, giving developers flexibility during development and testing phases.
Key functionalities include:
- Generating new wallets with private keys or mnemonics
- Restoring existing accounts from mnemonics or private keys
- Checking BNB and BEP20 token balances
- Executing secure transaction transfers with offline signatures
- Retrieving block information and transaction details
- Verifying transaction status through hash queries
Installation and Setup
To begin using the library, install it via Composer:
composer require fenguoz/bsc-phpFor specific version requirements:
composer require fenguoz/bsc-php ~1.0Account Management
The wallet component provides comprehensive account handling capabilities:
$wallet = new \Binance\Wallet();
// Generate account using private key
$wallet->newAccountByPrivateKey();
// Create account with mnemonic phrase
$wallet->newAccountByMnemonic();
// Restore account from existing mnemonic
$mnemonic = 'elite link code extra....';
$wallet->revertAccountByMnemonic($mnemonic);
// Retrieve address from private key
$privateKey = '5e9340935f4c02****f56563b8dffab368';
$wallet->revertAccountByPrivateKey($privateKey);Connecting to Binance Smart Chain
Method 1: Using BSC RPC Nodes
// Mainnet connection
$uri = 'https://bsc-dataseed1.binance.org';
$api = new \Binance\NodeApi($uri);
// Testnet connection
// $uri = 'https://data-seed-prebsc-1-s1.binance.org:8545/';
// $api = new \Binance\NodeApi($uri, null, null, 'testnet');Method 2: Utilizing Bscscan API
$apiKey = 'QVG2GK41A****RQ4XUQZCX';
$api = new \Binance\BscscanApi($apiKey);Working with BNB and BEP20 Tokens
Initialize the BNB and BEP20 handlers:
$bnb = new \Binance\Bnb($api);
$config = [
'contract_address' => '0x55d398326f99059fF775485246999027B3197955', // USDT BEP20 example
'decimals' => 18,
];
$bep20 = new \Binance\BEP20($api, $config);Balance Checking and Transactions
Retrieve asset balances for any address:
$address = '0x1667ca2c7****021be3a';
$bnbBalance = $bnb->bnbBalance($address);
$tokenBalance = $bep20->balance($address);Execute secure transfers with offline signatures:
$from = '0x1667ca2c7****021be3a';
$to = '0xd8699f0****b60eef021';
$amount = 0.1;
// BNB transfer
$bnb->transfer($from, $to, $amount);
// BEP20 token transfer
$bep20->transfer($from, $to, $amount);Blockchain Data Retrieval
Access comprehensive blockchain information:
// Get latest block number
$latestBlock = $bnb->blockNumber();
$latestBlockBEP20 = $bep20->blockNumber();
// Query block information by block number
$blockID = 24631027;
$blockData = $bnb->getBlockByNumber($blockID);
$blockDataBEP20 = $bep20->getBlockByNumber($blockID);
// Retrieve transaction receipt by hash
$txHash = '0x4dd20d01af4c621d2f****77988bfb245a18bfb6f50604b';
$receipt = $bnb->getTransactionReceipt($txHash);
$receiptBEP20 = $bep20->getTransactionReceipt($txHash);
// Get transaction details by hash
$transaction = $bnb->getTransactionByHash($txHash);
$transactionBEP20 = $bep20->getTransactionByHash($txHash);
// Check transaction status
$status = $bnb->receiptStatus($txHash);
$statusBEP20 = $bep20->receiptStatus($txHash);Implementation Best Practices
When integrating this library into production environments, consider these essential practices:
- Always secure private keys and mnemonics using environment variables or secure storage solutions
- Implement proper error handling for network requests and transaction failures
- Use testnet environments for development and testing before deploying to mainnet
- Monitor gas prices and network conditions for optimal transaction timing
- Implement rate limiting when using public RPC nodes to avoid being throttled
For advanced implementation strategies and real-time monitoring tools, consider to explore comprehensive development resources that can enhance your blockchain development workflow.
Frequently Asked Questions
What is the difference between BNB and BEP20 tokens?
BNB is the native cryptocurrency of the Binance Smart Chain, used for transaction fees and network operations. BEP20 tokens are digital assets built on the BSC network that follow a specific token standard, similar to ERC-20 tokens on Ethereum. Both can be managed using the bsc-php library.
How do I choose between RPC nodes and Bscscan API?
RPC nodes provide direct access to the blockchain and are suitable for most operations including transaction broadcasting. Bscscan API offers enhanced historical data and analytics but may have rate limits. For most applications, starting with RPC nodes is recommended, with Bscscan API for supplemental data needs.
What security measures should I implement when using this library?
Always store private keys and sensitive information securely using environment variables or encrypted storage. Implement proper error handling, validate all inputs, and use hardware wallets for significant fund management. Regularly update the library to benefit from security patches.
Can I use this library for both mainnet and testnet development?
Yes, the library supports both environments. Use the mainnet RPC endpoints for production applications and testnet endpoints during development. This allows you to test functionality without risking real assets.
How do I handle transaction failures and errors?
Implement comprehensive error handling that checks transaction receipts for status. Monitor gas prices to ensure sufficient fees, and include retry logic for failed transactions. Always verify transaction success through receipt status checks before proceeding with dependent operations.
What are the common use cases for this PHP library?
Common applications include payment processing systems, wallet services, token management platforms, blockchain explorers, and automated trading tools. The library provides the foundation for any PHP-based application that needs to interact with Binance Smart Chain assets.