You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP页面通过Luno mybitx API查询比特币钱包余额?

Hey there! Let me share a complete, working PHP example to help you fetch your Luno wallet balance using the mybitx API. I’ll break it down so you understand each step—perfect for someone just getting started with this integration.

PHP Example: Fetch Luno Wallet Balance

First, make sure your API key has read access to your wallet balances (you can verify this in your Luno account settings when managing API keys).

Full Working Code

<?php
// Replace these with your actual Luno API credentials
$apiKey = 'YOUR_LUNO_API_KEY';
$apiSecret = 'YOUR_LUNO_API_SECRET';

// Luno API endpoint for fetching balance (use sandbox URL for testing: https://api-sandbox.luno.com/api/1/balance)
$apiUrl = 'https://api.luno.com/api/1/balance';

// Generate required authentication components
$nonce = (string)(time() * 1000); // Luno expects a millisecond timestamp as nonce
$signatureData = $nonce . $apiUrl;
$signature = hash_hmac('sha256', $signatureData, $apiSecret);

// Initialize cURL session
$ch = curl_init($apiUrl);

// Configure cURL options
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $apiKey,
        'X-Luno-Nonce: ' . $nonce,
        'X-Luno-Signature: ' . $signature
    ],
    CURLOPT_SSL_VERIFYPEER => true, // Keep this enabled for secure connections
]);

// Execute request and fetch response
$response = curl_exec($ch);
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// Handle cURL errors
if (curl_errno($ch)) {
    echo 'cURL Error: ' . curl_error($ch);
    curl_close($ch);
    exit;
}

curl_close($ch);

// Parse JSON response
$balanceData = json_decode($response, true);

// Handle API-level errors
if ($httpStatusCode !== 200 || isset($balanceData['error'])) {
    echo 'API Error: ' . ($balanceData['error'] ?? 'Unknown error occurred');
    exit;
}

// Display formatted balance data
echo '<h2>Your Luno Wallet Balances</h2>';
echo '<ul>';
foreach ($balanceData['balance'] as $wallet) {
    echo '<li>
            <strong>' . htmlspecialchars($wallet['asset']) . '</strong>: 
            Total = ' . htmlspecialchars($wallet['balance']) . ' | 
            Available = ' . htmlspecialchars($wallet['available']) . '
          </li>';
}
echo '</ul>';
?>

Key Details Explained

  • Authentication: Luno uses HMAC SHA256 signatures to secure requests. We generate a unique millisecond nonce and sign the combination of nonce + full API URL with your secret key to validate the request.
  • Environment Switch: Swap the $apiUrl to the sandbox endpoint if you want to test without affecting real funds.
  • Error Handling: The code checks for both cURL connection errors and API-specific errors (like invalid credentials or missing permissions) to make debugging easier.
  • Security: We keep CURLOPT_SSL_VERIFYPEER enabled to ensure the connection is encrypted—never disable this in production!

Quick Notes

  • Keep your API secret private—never expose it in client-side code or public repositories.
  • If you get an "invalid signature" error, double-check that your nonce is a millisecond timestamp and that you’re signing the exact combination of nonce + full API URL.

内容的提问来源于stack exchange,提问作者Charles Adeyemo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:03:44