Coinmarketcap PHP沙箱API调用无数据返回问题排查求助
Hey Erik, let's work through why your CoinMarketCap sandbox API test isn't returning any output. Here are common issues to check and fixes to try:
1. Add Curl Error Debugging
Your current code tries to print the decoded JSON directly, but if the Curl request fails entirely, $response will be false—and json_decode(false) returns null, which gives no output. Add error checking to see what's going wrong:
$response = curl_exec($curl); if($response === false){ echo "Curl Error: " . curl_error($curl); } else { print_r(json_decode($response)); }
This will reveal issues like SSL certificate validation failures (though the sandbox API should have a valid cert, some servers lack root certs) or connection timeouts.
2. Fix the Request Header Format
You used 'Accepts: application/json' in your headers, but the correct HTTP header is Accept (no trailing "s"). While some servers tolerate this, it can cause the API to return non-JSON responses that json_decode can't parse:
$headers = [ 'Accept: application/json', 'X-CMC_PRO_API_KEY: b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c' ];
3. Test with Smaller Parameter Values
Your limit=5000 might be hitting unforeseen sandbox limits or causing large response delays. Try reducing the limit to 10 first to simplify the test:
$parameters = [ 'start' => '1', 'limit' => '10', 'convert' => 'USD' ];
4. Full Debug-Friendly Test Code
Here's the adjusted code combining all these fixes, plus raw response output to verify what the API is sending back:
<?php $url = 'https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'; $parameters = [ 'start' => '1', 'limit' => '10', 'convert' => 'USD' ]; $headers = [ 'Accept: application/json', 'X-CMC_PRO_API_KEY: b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c' ]; $qs = http_build_query($parameters); $request = "{$url}?{$qs}"; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $request, CURLOPT_HTTPHEADER => $headers, CURLOPT_RETURNTRANSFER => 1, // Uncomment this temporarily if you hit SSL cert issues (not recommended for production) // CURLOPT_SSL_VERIFYPEER => false )); $response = curl_exec($curl); if($response === false){ echo "Curl Error: " . curl_error($curl); } else { echo "Raw API Response: <br>" . $response . "<br><br>"; print_r(json_decode($response)); } curl_close($curl); ?>
Run this code—if the API returns an error message (like invalid key or parameter issues), the raw response will show you exactly what's wrong.
内容的提问来源于stack exchange,提问作者Erik

