首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用PHP调用Coinmarketcap

用PHP调用Coinmarketcap
EN

Stack Overflow用户
提问于 2021-11-05 17:44:47
回答 1查看 431关注 0票数 0

我尝试了我的第一个API调用,但是仍然有问题。我添加了我的API-键,选择了符号,并试图反映价格。但它仍然是无效的。但我的回声仍然是0。也许有人告诉我我做错了什么。谢谢!

代码语言:javascript
复制
    <?php

$coinfeed_coingecko_json = file_get_contents('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?symbol=ETH');
$parameters = [
  'start' => '1',
  'limit' => '2000',
  'convert' => 'USD'
];

$headers = [
  'Accepts: application/json',
  'X-CMC_PRO_API_KEY: XXX'
];

$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL

$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
  CURLOPT_URL => $request,            // set the request URL
  CURLOPT_HTTPHEADER => $headers,     // set the headers 
  CURLOPT_RETURNTRANSFER => 1         // ask for raw response instead of bool
));

$response = curl_exec($curl); // Send the request, save the response
print_r(json_decode($response)); // print json decoded response
curl_close($curl); // Close request

$coinfeed_json = json_decode($coinfeed_coingecko_json, false);

$coinfeedprice_current_price = $coinfeed_json->data->{'1'}->quote->USD->price;


?>
 <?php  echo $coinfeedde = number_format($coinfeedprice_current_price, 2, '.', '');  ?>

API:https://coinmarketcap.com/api/v1/#operation/getV1CryptocurrencyListingsLatest

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-05 19:02:48

在您的代码中有很多事情要做。

首先,$json->data[0]->quote->USD->price

  • Fourth;未定义json对象(
  • ),第二,您发出了两个请求,其中一个请求已删除了
  • 第三次请求;您可以通过$url访问json对象,我删除了无效的请求params

我改变了一些东西来使它发挥作用:

代码语言:javascript
复制
$url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";

$headers = [
  'Accepts: application/json',
  'X-CMC_PRO_API_KEY: ___YOUR_API_KEY_HERE___'
];

$request = "{$url}"; // create the request URL

$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
  CURLOPT_URL => $request,            // set the request URL
  CURLOPT_HTTPHEADER => $headers,     // set the headers 
  CURLOPT_RETURNTRANSFER => 1         // ask for raw response instead of bool
));

$response = curl_exec($curl); // Send the request, save the response
$json = json_decode($response);
curl_close($curl); // Close request

var_dump($json->data[0]->quote->USD->price);
var_dump($json->data[1]->quote->USD->price);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69857291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档