Introduction
As Solana continues to grow as one of the fastest and most efficient blockchain networks, many developers are building wallets, payment gateways, exchanges, and blockchain applications that need to retrieve wallet balances programmatically.
Fortunately, checking the native SOL balance of a wallet is straightforward thanks to Solana's JSON-RPC API. Unlike Ethereum or the BNB Smart Chain, Solana returns wallet balances as decimal integers instead of hexadecimal values, making the process even simpler.
In this guide, you'll learn how to retrieve a Solana wallet's balance using PHP, understand what lamports are, and convert the returned value into the human-readable SOL balance users expect to see.
Understanding How Solana Stores Wallet Balances
Every Solana wallet stores its native SOL balance in the blockchain's smallest unit called a lamport.
- 1 Bitcoin = 100,000,000 satoshis
- 1 Ether = 1,000,000,000,000,000 wei
- Solana uses: 1 SOL = 1,000,000,000 lamports
The blockchain always stores balances as integers to eliminate floating-point precision errors. When you query a wallet balance, the RPC node returns the balance in lamports. Your application simply divides the returned value by 1,000,000,000 (10⁹) to calculate the displayed SOL balance.
Prerequisites
- PHP 7.4 or newer
- cURL enabled (or
file_get_contents()with URL wrappers) - Access to a Solana RPC endpoint
- A valid Solana wallet address
Public RPC endpoint: https://api.mainnet-beta.solana.com
Step 1: Create the JSON-RPC Request
Solana uses JSON-RPC to communicate with nodes. To retrieve a wallet balance, use the getBalance method.
Example request:
The only required parameter is the wallet address.
{
"jsonrpc": "2.0",
"id": 1,
"method": "getBalance",
"params": [
"6aZ9D8M6z6GJm5kNLpWfH8J6fd7Jx6vJH3Qv3QdT2gJ5"
]
}
Step 2: Send the Request Using PHP
The following example sends the request using cURL.
<?php
$rpc = "https://api.mainnet-beta.solana.com";
$request = [
"jsonrpc" => "2.0",
"id" => 1,
"method" => "getBalance",
"params" => [
"6aZ9D8M6z6GJm5kNLpWfH8J6fd7Jx6vJH3Qv3QdT2gJ5"
]
];
$ch = curl_init($rpc);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [ "Content-Type: application/json" ],
CURLOPT_POSTFIELDS => json_encode($request)
]);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
Step 3: Read the Response
A successful response looks like this:
{
"jsonrpc": "2.0",
"result": {
"context": { "slot": 123456789 },
"value": 2154864200
},
"id": 1
}The important field is value: 2154864200
Unlike Ethereum-compatible blockchains, this value is already returned as a decimal integer. No hexadecimal conversion is necessary.
Step 4: Convert Lamports to SOL
The returned integer represents the balance in lamports. Convert it to SOL by dividing by 1,000,000,000.
$lamports = $response['result']['value'];
$balance = bcdiv((string)$lamports, '1000000000', 9);
echo $balance; // 2.154864200Output: 2.154864200 SOL
The wallet therefore contains 2.154864200 SOL.
Complete PHP Example
Putting everything together:
<?php
$rpc = "https://api.mainnet-beta.solana.com";
$request = [
"jsonrpc" => "2.0",
"id" => 1,
"method" => "getBalance",
"params" => [
"6aZ9D8M6z6GJm5kNLpWfH8J6fd7Jx6vJH3Qv3QdT2gJ5"
]
];
$ch = curl_init($rpc);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [ "Content-Type: application/json" ],
CURLOPT_POSTFIELDS => json_encode($request)
]);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
$lamports = $response['result']['value'];
$balance = bcdiv((string)$lamports, '1000000000', 9);
echo "SOL Balance: " . $balance;
// SOL Balance: 2.154864200Using file_get_contents() Instead of cURL
If your server allows HTTP stream wrappers, you can perform the same request without using cURL.
$rpc = "https://api.mainnet-beta.solana.com";
$request = [
"jsonrpc" => "2.0",
"id" => 1,
"method" => "getBalance",
"params" => [ $walletAddress ]
];
$options = [
"http" => [
"method" => "POST",
"header" => "Content-Type: application/json\r\n",
"content" => json_encode($request)
]
];
$response = file_get_contents($rpc, false, stream_context_create($options));
$response = json_decode($response, true);This approach works well for lightweight scripts, while cURL generally offers greater flexibility for production applications.
Common Mistakes
Forgetting the Lamport Conversion
The returned value is not the SOL balance displayed in wallets.
- Returned Value: 2154864200
- Displayed Balance: 2.154864200 SOL
Always divide by 10⁹.
Using Ethereum RPC Methods
Developers familiar with Ethereum sometimes attempt to call methods such as eth_getBalance. These methods do not exist on Solana. Instead, use getBalance.
Confusing SOL With SPL Tokens
The getBalance method retrieves only the native SOL balance. If you need balances for SPL tokens such as USDC, USDT, BONK, or other Solana tokens, you'll need to query the wallet's associated token accounts using methods like getTokenAccountsByOwner or getTokenAccountBalance.
Using an Unreliable Public RPC
The public Solana RPC endpoint is suitable for testing and development, but production applications often require higher reliability and rate limits. Using a dedicated RPC provider helps ensure faster response times and greater availability.
SOL vs SPL Tokens
It's important to distinguish between native SOL and SPL tokens.
| Feature | SOL | SPL Token |
|---|---|---|
| Stored directly in wallet | ✅ Yes | ❌ No |
| Requires smart contract | ❌ No | ✅ Yes |
| Retrieved using getBalance() | ✅ Yes | ❌ No |
| Stored in token account | ❌ No | ✅ Yes |
Understanding this distinction helps developers choose the correct RPC method depending on the asset they're querying.
Conclusion
Retrieving a Solana wallet balance in PHP is a simple process thanks to the blockchain's JSON-RPC API. By sending a getBalance request to a Solana node, your application receives the wallet's balance in lamports. Dividing that integer by 1,000,000,000 converts it into the human-readable SOL balance displayed by wallets and exchanges.
Unlike Ethereum-compatible blockchains, Solana returns balances as decimal integers instead of hexadecimal values, eliminating the need for hexadecimal conversion. Combined with PHP's built-in networking capabilities and BCMath for precise arithmetic, this makes it straightforward to build reliable wallet applications, payment systems, blockchain explorers, and cryptocurrency services that interact with the Solana network.