-1

I got a problem with the GetUserStatsForGame Api. I used http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=<<KEY>>&steamid=<<PROFILEID>>.

How can i get the the playerstats?

I already tried this but it dosent work :/

 if(!$_SESSION['steamid'] == ""){
    include("settings.php");
    if (empty($_SESSION['steam_uptodate']) or $_SESSION['steam_uptodate'] == false or empty($_SESSION['steam_personaname'])) {

        @ $url = file_get_contents("http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=".$steamauth['apikey']."&steamids=".$_SESSION['steamid']);
        $content = json_decode($url, true);
        $_SESSION['total_kills'] = $content['playerstats']['stats']['total_kills'][0]['value'];
        $_SESSION['steam_steamid'] = $content['response']['players'][0]['steamid'];


}

    $csgoprofile['total_kills'] = $_SESSION['total_kills'];
}

Please help!

ideasia
  • 3
  • 6
  • What does the output JSON look like? Quick guess, should `$content['playerstats'][..` be `$content['response']['playerstats'][..`? – Halcyon Aug 25 '15 at 12:45
  • `{ "playerstats": { "steamID": "17343684926432", "gameName": "ValveTestApp260", "stats": [ { "name": "total_kills", "value": 41269 }, { "name": "total_deaths", "value": 41720 },` – ideasia Aug 25 '15 at 13:00

1 Answers1

1

stats is a list of name/value pairs so $content['playerstats']['stats']['total_kills'] isn't going to work.

The path to the total_kills value is $content['playerstats']['stats'][0]['value'] but you can't rely on total_kills being at 0.

You can do:

$key = null;
foreach ($content['playerstats']['stats'] as $key => $stat) {
    if ($stat["name"] === "total_kills") {
        $key = $index;
    }
}

$total_kills = $content['playerstats']['stats'][$key]['value'];

If you also want to read the other stats you could do something like:

$stats = array();
foreach ($content['playerstats']['stats'] as $stat) {
    $stats[$stat["name"]] = $stat["value"];
}

$total_kills = $stats["total_kills"];
$total_deaths = $stats["total_deaths"];
Halcyon
  • 57,230
  • 10
  • 89
  • 128