简体   繁体   English

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

[英]can't get right api result from json (using json_decode)

I want to print result from API... But it is not working... I don't know what is wrong I tried to do it but got this and it is not working:我想从 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";
                                };
?>

Extra if you want to see the json response this is the link: https://api.battlemetrics.com/servers/5090469如果您想查看 json 响应,请附加以下链接: https : //api.battlemetrics.com/servers/5090469

Thanks in advance for your tips and help.在此先感谢您的提示和帮助。

Looks like you are just missing a key in your if statement.看起来您只是在 if 语句中缺少一个键。

It should be: if($response['data']['attributes']['status'] == "online")应该是: if($response['data']['attributes']['status'] == "online")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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