简体   繁体   中英

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:

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

Thanks in advance for your tips and help.

Looks like you are just missing a key in your if statement.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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