ideasia ideasia - 6 months ago 152
PHP Question

CS:GO Stats Api [GetUserStatsForGame]

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!

Answer

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"];
Comments