【发布时间】:2011-10-17 17:05:47
【问题描述】:
考虑以下用于从缓存的 RSS(XML) 提要中缓存对象的函数原型:
function cacheObject($xml,$name,$age = 3600)
{
// directory in which to store cached files
$cacheDir = "cache/";
// cache filename
$filename = $cacheDir.$name;
// default to fetch the file
$cache = true;
// but if the file exists, don't fetch if it is recent enough
if (file_exists($filename))
{
$cache = (filemtime($filename) < (time()-$age));
}
// fetch the file if required
if ($cache)
{
$item = $xml->channel->item;
file_put_contents($filename,serialize($item));
// update timestamp to now
touch($filename);
}
// return the cache filename
return unserialize(file_get_contents($filename));
}
函数调用如下:
$urlD = "http://somerss.php";
$xmlD = simplexml_load_file(cacheFetch($urlD,'cachedfeedD.xml',3600));
$itemD = '';
if($xmlD === FALSE)
{$itemD = '';}
else
{$itemD = cacheObject($xmlD,'cacheobjectD',3600);}
$urlM = "somerss2.php";
$xmlM = simplexml_load_file(cacheFetch($urlM,'cachedfeedM.xml',3600));
$itemM = '';
if($xmlM === FALSE)
{$itemM = '';}
else
{$itemM = cacheObject($xmlM,'cacheobjectM',3600);}
我收到以下错误:
Fatal error: Uncaught exception 'Exception'
with message 'Serialization of 'SimpleXMLElement' is not allowed' in C:\xampp\htdocs\sitefinal\cacheObject.php:20 Stack trace: #0 C:\xampp\htdocs\sitefinal\cacheObject.php(20): serialize(Object(SimpleXMLElement))
非常感谢任何帮助使该程序正常工作。
【问题讨论】: