0

I'm using cURL to query a minecraft server so I can display certain information about the server.

code:

$url = URL . '/api.php';
$params= 'case=getinfo';

$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$gameinfo = curl_exec($ch);

var_dump($gameinfo);

If I dump the returned data I get:

string(264) """{"hostname":"A Minecraft Server","gametype":"SMP","game_id":"MINECRAFT","version":"1.9","plugins":"CraftBukkit on Bukkit 1.9-R0.1-SNAPSHOT: PlayerData 1.0","map":"world","numplayers":"0","maxplayers":"200","hostport":"25565","hostip":"192.168.1.20","players":[]}"

I've tried using the the data in the following ways:

echo $gameinfo["hostname"];
echo $gameinfo->hostname;
echo $gameinfo[0]["hostname"];
echo $gameinfo[0]->hostname;
using foreach($gameinfo as $info)

for each of those I either get illegal string or non-object

So how can I use?

Joel E
  • 103
  • 1
  • 7

1 Answers1

0

Looks like your response is JSON which you need PHP to decode first before you use it as an array. Also trim the quotes off

$gameinfo = curl_exec($ch);
$gameinfo = json_decode(trim($gameinfo, '"'), true);

var_dump($gameinfo);