我是新手,我正在尝试使用PHP提取某些JSON API。在这种情况下,它是ETH_USD。下面是该API的代码段。
{“ symbol”:“ ETH_USD”,“ volumen24hours”:398.18320159,“ ask”:235.9,“ bid”:212.50000001,“ lastPrice”:213.48283633},{“ symbol”:“ ETH_EUR”,“ volumen24hours”: 101.79246082,“问”:191.51196436,“出价”:191.51196436,“最后价格”:191.51196436},{“符号”:“ ETH_ARS”,“ volumen24hours”:56.42036715,“问”:9365.12240119,“出价”:8952,“最后价格” “:9365.12240119},
以下是我的PHP代码,直到现在。我如何获取ETH_USD数据?任何帮助表示赞赏。
<?php
$url = "https://www.website.com/api/ticker?format=json";
$fgc = file_get_contents($ur);
$json = json_decode($fgc, true);
$price = $json["lastPrice"];
$high = $json["ask"];
$low = $json["bid"];
$open = $json["open"];
if($open < $price){
//price went up
$indicator = "+"
$change = $price - $open;
$percent = $change / $open;
$percent = $percent * 100;
$percentChange = $indicator.number_format($percent, 2);
$color = "green";
}
if($open > $price){
//price went down
$indicator = "-"
$change = $open - $price;
$percent = $change / $open;
$percent = $percent * 100;
$percentChange = $indicator.number_format($percent, 2);
$color = "red";
}
?>
答案 0 :(得分:0)
您可以为此目的使用array_filter:
$json = '[{
"symbol": "ETH_USD",
"volumen24hours": 398.18320159,
"ask": 235.9,
"bid": 212.50000001,
"lastPrice": 213.48283633
}, {
"symbol": "ETH_EUR",
"volumen24hours": 101.79246082,
"ask": 191.51196436,
"bid": 191.51196436,
"lastPrice": 191.51196436
}, {
"symbol": "ETH_ARS",
"volumen24hours": 56.42036715,
"ask": 9365.12240119,
"bid": 8952,
"lastPrice": 9365.12240119
}]';
$new = array_filter(json_decode($json, true), function ($var) {
return ($var['symbol'] == 'ETH_USD');
});