Reputation: 332
I have a small website jamesmcnee.co.uk and the page loading times of it are quite high. It can take as long as 8 - 12s to load a page. I think that it is something to do with the PHP I have created to interact with the Steam API. I have these files:
SteamWidget.php:
$api = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=". $key ."&steamids=76561198014955377&format=json";
$json = file_get_contents($api);
$schemaProfile = json_decode($json, true);
$api = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=". $key ."&steamid=76561198014955377&format=json";
$json = file_get_contents($api);
$schemaGames = json_decode($json, true);
echo '<div id="sideSteamProfile">';
echo '<h2>On Steam</h2>';
echo '<img src="'; echo $schemaProfile['response']['players'][0]['avatarfull']; echo '" alt="Steam Avatar Icon" />';
echo '<p>Steam Name:<a href="'; echo $schemaProfile['response']['players'][0]['profileurl']; echo '">';
echo $schemaProfile['response']['players'][0]['personaname']; echo '</a></p>';
if($schemaProfile['response']['players'][0]['personastate'] == "0"){
echo '<p>Currently: <font color="red">Offline</font></p>';
}
else{
echo '<p>Currently: <font color="green">Online</font></p>';
}
echo '<p>Last Online:'; echo gmdate("d-m-Y H:i", $schemaProfile['response']['players'][0]['lastlogoff']); echo '</p>';
echo '<p>Number of Games:'; echo $schemaGames['response']['game_count']; echo '</p>';
echo '<p>Most Played Game:'; echo getMostPlayed(); echo '</p>';
echo '<p>Total Hours:'; echo getHoursPlayed(); '</p>';
echo '</div> <!-- Closes the sideSection div -->'; ?>
The second one is called MostPlayed.php:
<?php
include 'ApiKey.php';
function getMostPlayed()
{
$GameWithMostHours = "None";
$api = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=". $key ."&steamid=76561198014955377&format=json";
$json = file_get_contents($api);
$schemaGames = json_decode($json, true);
$NumberOfGames = $schemaGames['response']['game_count'];
$CurrentlyHighestHours = 0;
for ($x = 0; $x < $NumberOfGames; $x++){
$CheckHighest = $schemaGames['response']['games'][$x]['playtime_forever'];
if ($CheckHighest > $CurrentlyHighestHours){
$CurrentlyHighestHours = $CheckHighest;
$GameWithMostHours = $schemaGames['response']['games'][$x]['name'];
}
}
return $GameWithMostHours;
}
function getHoursPlayed()
{
$api = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=". $key ."&steamid=76561198014955377&format=json";
$json = file_get_contents($api);
$schemaGames = json_decode($json, true);
$NumberOfGames = $schemaGames['response']['game_count'];
$TotalNumberOfHours = 0;
for ($x = 0; $x < $NumberOfGames; $x++){
$TotalNumberOfHours += $schemaGames['response']['games'][$x]['playtime_forever'];
}
$TotalNumberOfHours = sprintf("%02dh %02dm", floor($TotalNumberOfHours/60), $TotalNumberOfHours%60);
return $TotalNumberOfHours;
}
?>
So two reletively simple PHP files, so why the increase in loading speed. As I mentioned I think it has something to do with the for loops I am using but I dont see another way to do this and in another language such as JAVA, these loops would take a fraction of a second. Any advice is appreciated!
Thanks James McNee.
Upvotes: 1
Views: 396
Reputation: 2736
Load the page without doing those calls to that API. Have an AJAX call that runs on document ready to a script that gets the data, processes it and returns the html that you want to display.
Upvotes: 1