douzhang7603 2016-07-14 14:22
浏览 304
已采纳

从.json文件循环结果时加载页面缓慢

I am making a website for a gaming group and they would like to add their guild information on their website using the API the game developers provide. The APIs I'm working with are the following:

https://wiki.guildwars2.com/wiki/API:1/item_details https://wiki.guildwars2.com/wiki/API:2/guild/:id/treasury

Right now, I have a table to show what items are in the treasury, how many, and how many are needed. It looks like this:

Item name     |     Quantity     |    Needed
-----------------------------------------------------
Iron Ore      |     150          |    1500
Mithril Ingot |     134          |    1000
etc...

And this is my PHP code:

// This gets a particular item.
function getItem($id) {
    $response = file_get_contents("https://api.guildwars2.com/v1/item_details.json?item_id=".$id);
    $response = json_decode($response, false);
    return $response;
}
// This gets the name if item 123
// echo getItem(123)->name;

// Begin table.
echo '<table class="table table-striped">';

$treasuryItem = file_get_contents('https://api.guildwars2.com/v2/guild/B95A3B40-A764-4648-8EE6-39549E922A99/treasury?access_token=358AC6CB-0596-D64F-88D5-5CFA9AA27AAA273F3C6A-BC9E-47EE-AA28-63565C3EFEEE');
$treasuryItem = json_decode($treasuryItem);

foreach($treasuryItem as $key => $treasuryItem) {

    echo '<tr>';
    echo '<td>'.getItem($treasuryItem->item_id)->name.'</td>';   // This is where I think the trouble lies.
    echo '<td>'.$treasuryItem->count.'</td>';
    echo '<td>';
        foreach($treasuryItem->needed_by as $key => $treasuryItem) {
            echo $treasuryItem->count.'<br />';
        }
    echo '</td>';
    echo '</tr>';

}
echo '</table>';

As it stands now, it works and the info is displayed as I'd like it to be. My problem is that it takes a very long time for the page to load. I think the problem is that the function that calls the Item API file is being looped in the foreach statement. What alternative method can I use to prevent this from happening?

  • 写回答

1条回答 默认 最新

  • douping3860 2016-07-14 14:45
    关注

    The Link to the Items seems public, and i thing items wont change after set up. Maybe they change after a patch or so.

    If you keep that in mind, you can hold the informations on your server. Change the function to this:

    function getItem($id,$forceDownload=false) {
       $response=null;
       $file = "item{$id}.json";
       if(!file_exists($file) || $forceDownload){
           $response = file_get_contents("https://api.guildwars2.com/v1/item_details.json?item_id=".$id);
           file_put_contents($file,$response);
       }
       if(!$response){
        $response = file_get_contents($file);
       }
       return json_decode($response, false);
    }
    

    Now for each Item a file would be saved e.g. item123.json, next time on pageload it will not be fetched from the WEB again, instead it uses the given file. That will be quit faster then.

    If you want to reload all files from WEB use the second parameter $forceDownload and set it to true. Then all files will be loaded again.

    And maybe set up a better dir like "../items/item{$id}.json"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作