无法从 json 获得正确的 api 结果(使用 json_decode)

我想从 API 打印结果......但它不工作......我不知道有什么问题我试图这样做但得到了这个并且它不工作:


<?php

$api = 'https://api.battlemetrics.com/servers/'; // the main API for servers.

$server = '5090469'; // the number of the server you will get the info from most of thime something like 5484856.

$api_full = $api . $server; // bringing them together.

$json = file_get_contents("$api_full"); // Putting the content of the file in a variable.

$response = json_decode($json, true); // decode the JSON feed


if ($response['data']['status'] == "online") {

                                    echo "ONLINE"; 

                                } else {

                                    echo "OFFLINE";

                                };

?>

如果您想查看 json 响应,请附加以下链接:https ://api.battlemetrics.com/servers/5090469


提前感谢您的提示和帮助。


肥皂起泡泡
浏览 111回答 1
1回答

慕村9548890

看起来您只是在 if 语句中缺少一个键。它应该是:&nbsp;if($response['data']['attributes']['status'] == "online")
打开App,查看更多内容
随时随地看视频慕课网APP