我需要从这里获取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'];
答案 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;