我有以下代码:
<?php
$items = simplexml_load_file("http://foo.xml");
foreach ($items->bar->baz as $item) {
echo $item;
}
?>
我想缓存xml文件,所以我被告知要使用:
$cacheName = "foo.xml.cache";
// generate the cache version if it doesn't exist or it's too old!
$ageInSeconds = 24 * 60 * 60; // one day
if (!file_exists($cacheName) || filemtime($cacheName) > time() + $ageInSeconds)
{
$contents = file_get_contents("http://foo.xml");
file_put_contents($cacheName, $contents);
}
$dom = simplexml_load_file($cacheName);
我尝试创建一个在foreach循环之前调用的函数。我怎样才能做到这一点?谢谢!
答案 0 :(得分:2)
我认为这个表达是倒退的:
filemtime($cacheName) > time() + $ageInSeconds
我认为应该是
filemtime($cacheName) + $ageInSeconds < time()
或等同于:
filemtime($cacheName) < time() - $ageInSeconds