![](/img/trans.png)
[英]How to print following json data using foreach in 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.