簡體   English   中英

無法使用 php 獲取 coinmarket 的 api

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

我想為卡爾達諾硬幣制作一個價格檢查器,它適用於通用 api。 https://api.coinmarketcap.com/v1/ticker/

但我想使用這個 api,因為我不需要其他硬幣的信息。 https://api.coinmarketcap.com/v1/ticker/cardano

我用於第一個的代碼:

<!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>

更改為其他 api 似乎很容易,但我無法使其正常工作。

我的第二個代碼:

  <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);
});

對我有用,只需用/完成網址。 我認為由於 Web 服務器中的某些重定向規則,沒有最后一個斜杠是行不通的。

順便說一句,第一個示例將在 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;
    }
  }
});

這樣當找到 cardano id 時 for 就會停止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM