从php(或数组?)获取json数据

时间:2017-08-17 20:17:51

标签: php json server

我需要从这里获取JSON数据https://use.gameapis.net/mc/query/info/play.mineverge.net

我需要“在线”:126才能在我的网页上显示。

"players": {
    "online": 126,
    "max": 500

这是我当前的代码(不工作)获取JSON。难道我做错了什么?它显示的不仅仅是“在线”:

             $playeronline = file_get_contents ('https://use.gameapis.net/mc/query/info/' . $server); 
            echo $playeronline->players[1]; 
            echo $playeronline['online']; 

3 个答案:

答案 0 :(得分:1)

试试这个,

$playeronline = file_get_contents ('https://use.gameapis.net/mc/query/info/' . $server); 
$data=json_decode($playeronline,true);

echo $data['players']['online']; 

答案 1 :(得分:1)

这对我有用,我测试了它:

$str = file_get_contents ('https://use.gameapis.net/mc/query/info/play.mineverge.net');

$playersonline = json_decode( $str );

echo $playersonline->players->online;

答案 2 :(得分:0)

如上所述here,您需要使用json_decode功能,如下所示:

$playeronline = file_get_contents('https://use.gameapis.net/mc/query/info/' . $server); 

$obj = json_decode($playeronline );
echo $playersonline->players->online;
相关问题