PHP Steam Profile Widget

March 6, 2016 by Asbra — Leave a comment

This article will show you code, as well as a run-down of it, for my Steam profile widget.

If you simply want your own, visit https://dev.asbra.net/steam/ to generate it.

You can find the full code for this entire project on my GitHub repo

So, what I set out to make was a simple way to display a link to my Steam profile, as well as some statistics and status.

First of all you’re gonna need to get your Steam Web API Key

Retrieve Steam Profile ID number

We need to retrieve the Steam profile ID number, if user has given us his vanity name, or profile URL, we need to resolve it to a ID before we can retrieve data for the account.
To resolve a vanity ID to a Steam profile ID we use the Steam Web API endpoint ResolveVanityURL

// If input is already numeric, assume it's a valid Steam ID
if (is_numeric($profile_url)) {
  $steam_id = $profile_url;
}
// If the input is a profile URL, we extract the vanity ID
else if (strstr($profile_url, 'steamcommunity.com/id/') !== false) {
  $profile_url = rtrim($profile_url, '/');
  $profile_url = substr($profile_url, strrpos($profile_url, '/')+1);

  if (is_numeric($profile_url)) {
    $steam_id = $profile_url;
  }
  else {
    $url_resolve = "https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key={$api_key}&vanityurl={$profile_url}";
    $json        = json_decode(file_get_contents($url_resolve));

    if ($json->response->success != 1) {
      die('Failed to resolve nickname.');
    }

    $steam_id = $json->response->steamid;
  }
}
// Otherwise assume it's just the vanity ID, so resolve it
else {
  if (strpos($profile_url, '://') === false)
  {
    $url_resolve = "https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key={$api_key}&vanityurl={$profile_url}";
    $json        = json_decode(file_get_contents($url_resolve));

    if ($json->response->success != 1) {
      die('Failed to resolve nickname.');
    }

    $steam_id = $json->response->steamid;
  }
}

if (!is_numeric($steam_id)) {
  die('Steam ID error');
}

Now that we have a valid Steam ID we can query the Steam Web API to get profile details.

Querying Steam Web API

We will use the following endpoints
IPlayerService/GetRecentlyPlayedGames to get AppID of games played in the past 2 weeks
ISteamUser/GetPlayerSummaries to get nickname, country, online status, and other profile details
IPlayerService/GetOwnedGames to get list of owned games. I set the argument include_played_free_games=0 to skip f2p games

$url_played  = "https://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key={$api_key}&steamid={$steam_id}&format=json&count={$count}";
$url_summary = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={$api_key}&steamids={$steam_id}";
$url_owned   = "https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={$api_key}&steamid={$steam_id}&include_played_free_games=0&include_appinfo=1";

$played  = json_decode(file_get_contents($url_played));
$summary = json_decode(file_get_contents($url_summary));
$owned   = json_decode(file_get_contents($url_owned));

if (!$summary || !$summary->response || !$played || !$played->response) {
  die('Failed to get profile data!');
}

Parsing data from Steam Web API

Parse the data we got

$games_num = $owned->response->game_count;

$player = $summary->response->players[0];

$profile->nick          = $player->personaname;
$profile->link          = $player->profileurl;
$profile->avatar        = $player->avatar;
$profile->avatar_medium = $player->avatarmedium;
$profile->country       = isset($player->loccountrycode) ? $player->loccountrycode : '';
$profile->clan          = isset($player->primaryclanid) ? $player->primaryclanid : 0;
$profile->created       = isset($player->timecreated) ? date('Y-m-d', $player->timecreated) : 0;
$profile->seen          = isset($player->lastlogoff) ? $player->lastlogoff : null;
$profile->state         = $player->personastate;
$profile->in_game       = isset($player->gameid) ? true : false;
$profile->game_count    = $owned->response->game_count;

Set the text color for the name to correspond with the current online status. I couldn’t find if there’s a PersonaState for in-game, so made a different case for it.

if ($profile->in_game) {
  $profile->color = 'rgb(131,179,89)';
} else {
  switch ($profile->state)
  {
    case PersonaState::Online:
    case PersonaState::Busy:
    case PersonaState::Away:
    case PersonaState::Snooze:
    case PersonaState::LookingToTrade:
    case PersonaState::LookingToPlay:
      $profile->color = '#4787A0';
      break;
    case PersonaState::Offline:
    default:
      $profile->color = 'grey';
      break;
  }
}

Same or similar to Steams colors, green for in-game, blue for other status’, grey for offline.

I wanted to show on my Steam widget the number of games in my library I haven’t played, since there’s so many..

$max     = $owned->response->game_count;
$profile->not_played_count = 0;

// Iterate over all games
for ($i = 0; $i < $max; $i++) {
  $game = $owned->response->games[$i];

  // If the game has never been played
  if ($game->playtime_forever <= 0)
    $profile->not_played_count++;
}

$profile->not_played_perc = round(($profile->not_played_count/$profile->game_count)*100);

Get list of the recently (last 2 weeks) played games, they’re sorted by time played (descending)

$profile->played_2weeks = $played->response->total_count;

$max = ($played->response->total_count < $count ? $played->response->total_count : $count);

for ($i = 0; $i < $max; $i++)
{
  $game = $played->response->games[$i];

  // Cut the game name if it's long
  if (strlen($game->name) > 32) {
    $game->name = substr($game->name, 0, 30).'..';
  }

  // Generate link to store page
  $game->link = "http://steamcommunity.com/app/{$game->appid}";
  // Get the game image
  $game->image = "http://media.steampowered.com/steamcommunity/public/images/apps/{$game->appid}/{$game->img_icon_url}.jpg";

  $games[] = $game;
}

Get information about the currently played game

if ($profile->in_game)
{
  $game->appid  = $player->gameid;
  $game->link   = "http://steamcommunity.com/app/{$game->appid}";
  $game->image  = "http://media.steampowered.com/steamcommunity/public/images/apps/{$game->appid}/{$game->img_icon_url}.jpg";
  $game->logo   = "http://media.steampowered.com/steamcommunity/public/images/apps/{$game->appid}/{$game->img_logo_url}.jpg";
  $game->header = "http://cdn.akamai.steamstatic.com/steam/apps/{$game->appid}/header.jpg";
  $game->name   = $player->gameextrainfo;
}

Rendering the Steam profile widget

Then I just pass the data to my template system, save the HTML output to cache, and output it.

Asbra

Posts Facebook

Blogging out of many years of experience with gamehacking, programming, reverse-engineering and general tomfoolery.

No Comments

Be the first to start the conversation.

Leave a Reply