简体   繁体   English

在php中遍历JSON会减慢我的页面加载速度

[英]Looping through JSON in php is slowing down my page loading

I have a small website jamesmcnee.co.uk and the page loading times of it are quite high. 我有一个小型网站jamesmcnee.co.uk ,它的页面加载时间非常长。 It can take as long as 8 - 12s to load a page. 加载页面最多可能需要8-12s。 I think that it is something to do with the PHP I have created to interact with the Steam API. 我认为这与我创建的与Steam API交互的PHP有关。 I have these files: 我有这些文件:

  1. SteamWidget.php: 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("dmY 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: 第二个称为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. 因此有两个简单的PHP文件,为什么要增加加载速度。 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. 正如我提到的那样,我认为它与我使用的for循环有关,但是我看不到另一种方式来执行此操作,而在另一种语言(如JAVA)中,这些循环将花费一秒钟的时间。 Any advice is appreciated! 任何建议表示赞赏!

Thanks James McNee. 感谢James McNee。

Load the page without doing those calls to that API. 加载页面而不对那些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. 准备好在文档上运行的AJAX调用,以获取数据,处理数据并返回要显示的html的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM