简体   繁体   中英

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. https://api.coinmarketcap.com/v1/ticker/ .

But i want to use this api because i don't need the info of the other coins. 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.

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.

Bytheway, the first example would be more optimised with a break inside the if condition:

$.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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