Enge Enge -3 years ago 106
PHP Question

PHP JSON Decode with number

I want to Display some information from that API https://steamgaug.es/api/v2 on my Website

Thats my current Code:

$steamStatusJson = @file_get_contents("https://steamgaug.es/api/v2");
$steamStatus = json_decode($steamStatusJson);
$csgoStatus = $steamStatus->ISteamGameCoordinator->730->online;
$csgoplayerssearching = $steamStatus->ISteamGameCoordinator->730->stats->players_searching;

$csgoplayers = $steamStatus->ISteamGameCoordinator->730->stats->players_online;

I always get this error message:

FATAL ERROR syntax error, unexpected '730' (T_LNUMBER), expecting identifier (T_STRING) or variable

Thank you very much
With best reagrds

Answer Source

as lone as you are decoding your json as an object, you can not use numbers as a properties names

so, you need to this line :

$csgoStatus = $steamStatus->ISteamGameCoordinator->730->online;

should be as follows :

$csgoStatus = $steamStatus->ISteamGameCoordinator->{"730"}->online;
//                                                 ^^^^^^^

also the same with those lines :

$csgoplayerssearching = $steamStatus->ISteamGameCoordinator->{"730"}->stats->players_searching;
$csgoplayers =  $steamStatus->ISteamGameCoordinator->{"730"}->stats->players_online;

or, simply by decoding your json as an array

$steamStatus = json_decode($steamStatusJson, true);

and then you can access it as :

$csgoStatus = $steamStatus['ISteamGameCoordinator']['730']['online'];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download