我有一个小数组,它从Twitch.tv中获取信息并使用我创建的数组中的变量显示它。
<?php include 'header.php';
$streamers = array("NomadicTV","TheOneJat","greatbritishbg","Themastergamer94","Gecko194","slayus","skunkodor");
shuffle($streamers);
$random = shuffle($streamers);
$streamerscount = 0;
$streamtotal = count($streamers);
while (list(, $val) = each($streamers)) {
$url = "https://api.twitch.tv/kraken/streams/".$val;
$json = file_get_contents($url);
$json = json_decode($json);
$stream = $json->stream;
$channelAPI = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/'. $val));
$status = $channelAPI->status;
$name = $channelAPI->display_name;
$gameimg = "http://static-cdn.jtvnw.net/ttv-boxart/".$channelAPI->game . "-272x380.jpg";
$viewers = $streamsAPI->stream->viewers;
$followers = $channelAPI->followers;
$views = $channelAPI->views;
$avatar = $channelAPI->logo;
echo '<a href="cast.php?caster='.$val.'"/><img src="' . $avatar . '" class="streamers"/></a>';
}
?>
我的问题是,当页面加载时,它会在while列表中运行一段时间。有没有替代方案可以让我的页面加载更快?
由于
答案 0 :(得分:0)
您可以将结果缓存在文件中,例如:
$cacheFile = 'path/to/cache/folder/' . md5(serialize($shuffle));
$age = time() - filemtime(cacheFile);
$maxAge = 3600; // 1 hour
if(file_exists($cacheFile) && $age < $maxAge) {
echo file_get_contents($cacheFile);
} else {
ob_start();
/* here comes your loop */
$output = ob_get_clean();
file_put_contents($cacheFile, $output);
echo $output;
}
sys_get_temp_dir()
。