简体   繁体   English

无法使用 php 获取 coinmarket 的 api

[英]can't get the api of coinmarket work with php

I wanted to make a priceChecker for the Cardano coin and it works with the general api.我想为卡尔达诺硬币制作一个价格检查器,它适用于通用 api。 https://api.coinmarketcap.com/v1/ticker/ . https://api.coinmarketcap.com/v1/ticker/

But i want to use this api because i don't need the info of the other coins.但我想使用这个 api,因为我不需要其他硬币的信息。 https://api.coinmarketcap.com/v1/ticker/cardano . https://api.coinmarketcap.com/v1/ticker/cardano

the code i used for the first one:我用于第一个的代码:

<!DOCTYPE html>
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <title id="title"></title>
  </head>
  <table>
    <tr>
      <th>Cardano</th>
    </tr>
    <tr>
      <td id="cardano"></td>
    </tr>
  </table>

<script>
$.get("https://api.coinmarketcap.com/v1/ticker/", function(data, status) {
  for (var i = 0; i < data.length - 1; i++) {
    if (data[i].id == "cardano") {
      $("#title").html(data[i].price_usd);
      $("#cardano").html(data[i].price_usd);
    }
  }
});  
</script>
</body>
</html>

It seems really easy to change to the other api but i just can't get it work.更改为其他 api 似乎很容易,但我无法使其正常工作。

My code for the second one:我的第二个代码:

  <script>
$.get("https://api.coinmarketcap.com/v1/ticker/cardano", function(data, status) {
      $("#title").html(data[0].price_usd);
      $("#cardano").html(data[0].price_usd);
});  
</script>
$.get("https://api.coinmarketcap.com/v1/ticker/cardano/", function(data, status) {
  $("#title").html(data[0].price_usd);
  $("#cardano").html(data[0].price_usd);
});

works for me, just finishing the url with / .对我有用,只需用/完成网址。 I think without the final slash doesn't work due to some redirect rules in the web server.我认为由于 Web 服务器中的某些重定向规则,没有最后一个斜杠是行不通的。

Bytheway, the first example would be more optimised with a break inside the if condition:顺便说一句,第一个示例将在 if 条件中使用中断进行更优化:

$.get("https://api.coinmarketcap.com/v1/ticker/", function(data, status) {
  for (var i = 0; i < data.length - 1; i++) {
    if (data[i].id == "cardano") {
      $("#title").html(data[i].price_usd);
      $("#cardano").html(data[i].price_usd);
      break;
    }
  }
});

this way the for stops when the cardano id is found.这样当找到 cardano id 时 for 就会停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM