我有一个网站,加载非常慢,因为它集成了几个数据直接从流从kimonolabs.com(通过csv)。我对数据库不太熟悉,所以它通过纯php请求获取数据。
我知道最好的方法是将csv的导入到数据库,然后请求本地数据库,但我不想变得那么复杂-为了我的目的和谷歌搜索引擎优化我想要一个解决方案,其中生成的index.php将自动复制到index.html每小时-因为这是如何频繁的流得到更新。手动操作没有问题(只需在浏览器中复制开源代码&粘贴)。
示例PHP的源代码请求:
<?php $file_handle = fopen("kimonolabs.com/api/csv/xxx";, "r");
while (!feof($file_handle) )
{ $line_of_text = fgetcsv($file_handle, 1024);
if ($line_of_text[0] == 'EXAMPLE'){
print "<p>" . $line_of_text[0] . " " . $line_of_text[2] . "</p>";};
}
fclose($file_handle); ?>
谢谢!
也许最简单的选择是将现有的index.php
重命名为real_index.php
,并使index.php
像这样:
$cache = 'cache.html';
if(!is_readable($cache) || filemtime($cache) < time() - 3600) {
ob_start();
include 'real_index.php';
file_put_contents($cache, ob_get_clean());
}
readfile($cache);
设置cronjob是实现这一目标最直接的方法,因为您基本上是在尝试将数据输出从.php文件调度到index.html文件。
另一个变化是输出索引作为一个。php文件,而不是一个。html文件,使用memcache, Redis甚至phpFastCache与一个小时的过期时间为csv数据。
除此之外,我找不到直接的解决办法。
只是个想法。将其放入一个文件中,并将该文件与index.php放在一起,并设置一个cron以每小时执行该文件。这绝不是最好的办法。您必须考虑Ajax加载或缓存..
<?php
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en'r'n" .
"Cookie: foo=bar'r'n"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.yoururl.com/index.php', false, $context);
file_put_contents('index.html', $file);
?>