在php中循环使用JSON会减慢我的页面加载速度

时间:2015-03-15 17:55:03

标签: php json loops

我有一个小网站jamesmcnee.co.uk,其网页加载时间非常长。加载页面可能需要8到12秒。我认为这与我创建的用于与Steam API交互的PHP有关。我有这些文件:

  1. 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 -->'; ?>
    
  2. 第二个名为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;
        }
    ?>
    

    所以两个相对简单的PHP文件,所以为什么增加加载速度。正如我所提到的,我认为它与我正在使用的for循环有关,但我没有看到另一种方法来执行此操作,而在另一种语言(如JAVA)中,这些循环只需要几分之一秒。任何建议表示赞赏!

    感谢James McNee。

1 个答案:

答案 0 :(得分:1)

加载页面而不对该API进行调用。在文档上运行一个AJAX调用,该脚本可以获取数据,处理数据并返回要显示的html。