Skip to content

Instantly share code, notes, and snippets.

@msklywenn
Last active December 15, 2020 09:50
Show Gist options
  • Save msklywenn/309d3bf6eb8954c732491d1ca0c25273 to your computer and use it in GitHub Desktop.
Save msklywenn/309d3bf6eb8954c732491d1ca0c25273 to your computer and use it in GitHub Desktop.
Fetch & cache steam leaderboards to display on your website
<?php
// ref: https://partner.steamgames.com/doc/webapi/ISteamLeaderboards
$cache_filename = "leaderboard_cache";
$default_leaderboard = "Normal";
if (!file_exists($cache_filename) || time() - filemtime($cache_filename) > 3600)
{
$max = 10; // how many displayed scores
$key = ""; // steam webapi key, to get it: https://partner.steamgames.com/doc/webapi_overview/auth#create_publisher_key
$appid = ""; // your game's app id
// the names and corresponding leaderboard IDs
// get the leaderboard IDs with https://partner.steam-api.com/ISteamLeaderboards/GetLeaderboardsForGame/v2/
$leaderboards = [ "Easy" => "2025616", "Normal" => "2025617", "Hard" => "2025618" ];
foreach ($leaderboards as $name => $id)
{
// fetch scores
$uri = "https://partner.steam-api.com/ISteamLeaderboards/GetLeaderboardEntries/v1/?key="
.$key."&appid=".$appid."&rangestart=0&rangeend=".$max."&datarequest=0&leaderboardid=".$id;
$data = file_get_contents($uri);
$leaderboard = json_decode($data);
$players = "";
foreach ($leaderboard->leaderboardEntryInformation->leaderboardEntries as $entry)
$players .= $entry->steamID.",";
// fetch player names
$uri = "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=".$key."&steamids=".$players;
$data = file_get_contents($uri);
$players = json_decode($data);
// add names to scores
for ($i = 0; $i < $max; $i++)
{
$entry = $leaderboard->leaderboardEntryInformation->leaderboardEntries[$i];
$personaname = "";
foreach ($players->response->players as $player)
if ($player->steamid == $entry->steamID)
$entry->name = $player->personaname;
}
$results[$name] = $leaderboard->leaderboardEntryInformation->leaderboardEntries;
}
// cache the data because
file_put_contents($cache_filename, serialize($results));
}
else
{
$results = unserialize(file_get_contents($cache_filename));
}
// display leaderboards
foreach ($results as $name => $leaderboard)
{
$i = 1;
$active = $name == $default_leaderboard ? "active" : "";
// using bootstrap tables
print '<div class="item '.$active.' table-responsive"><table class="table">';
print '<caption class="text-center">'.$name.'</caption>';
print '<thead><tr><th class="text-right">Rank</th><th>Name</th><th>Score</th></tr></thead><tbody>';
foreach ($leaderboard as $entry)
printf('<tr><th class="text-right" scope="row">%s</th><td>%s</td><td>%s</td></tr>', $i++, $entry->name, $entry->score);
print '</tbody></table></div>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment