Picking json object inside another object

Asked

Viewed 555 times

2

I need to pick up the object "online" that is inside the object "players" more I can’t catch... I can only pick up objects that are "At the root" json where I need to pick up:

{
    "status": true,
    "hostname": "skyminigames.com.br",
    "port": 25565,
    "protocol": "tcp",
    "ping": 126,
    "players": {
        "online": 3376,
        "max": 8500
    },
    "cached": false
}

Code I’m using:

<?php 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, 'https://use.gameapis.net/mc/query/players/skyminigames.com.br');
        $result = curl_exec($ch);
        curl_close($ch);

        $obj = json_decode($result);
        echo "$obj->players->online";
?>
  • Do not forget to approve the answer of Laerte. Since it is correct.

1 answer

3


Try the echo double quote:

echo $obj->players->online;
  • It can’t be used like this... you have to take this data from the url it was... because this data constantly updates.

  • I got it, I just used echo $obj->players->online; the quotes was the mistake I guess.

  • Yes, that’s what I meant, you were giving echo $obj->players->online as a string, my answer was you remove the double quotes that were around echo, I just left the result fixed for you to better understand rs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.