繁体   English   中英

PHP:DOMDocument :: Load():I / O警告:无法加载外部实体(外部RSS源和缓存文件)

[英]PHP:DOMDocument::load(): I/O warning : failed to load external entity (external rss feed and cache file)

我需要在页面上显示外部RSS feed的最新条目。 我制作了一个使用缓存文件的简单rss阅读器。

它可以完美地在我的测试环境中运行,但是当我将其放到网站上时,每次必须写入缓存文件时都会出错:

警告:DOMDocument :: load():I / O警告:无法在第45行的... / index.php中加载外部实体“ ... / rssRadio.xml.cache”

这是页面的代码。 第45行是最后一条:

  $cacheName = 'cache/rss.xml.cache'; $ageInSeconds = (3600*2); // two hour if(!file_exists($cacheName) || filemtime($cacheName) < time() - $ageInSeconds ) { $contents = file_get_contents($RSSURL); file_put_contents($cacheName, $contents); } $rss = new DOMDocument(); if (!$rss->load($cacheName)) { throw new Exception('Impossibile caricare i podcast'); } $feed = array(); foreach ($rss->getElementsByTagName('item') as $node) { 

...

如果我重新加载页面,该错误消失了,并且显示了内容,因此缓存文件可以正常工作。

看起来脚本正在尝试在file_put_contents调用结束之前读取缓存文件。 但这是不可能的,因为这是阻塞的I / O调用...对吗? 有任何想法吗?

干杯

问题可能出在服务器延迟更新索引树上。 您可以添加usleep(100);来解决它usleep(100); file_put_contents之后。

但是,我认为最好的解决方案是这样的:

if(!file_exists($cacheName) || filemtime($cacheName) < time() - $ageInSeconds ) 
{
    $contents = file_get_contents( $RSSURL );
    file_put_contents( $cacheName, $contents );
}
else
{
    $contents = file_get_contents( $cacheName );
}
$rss = new DOMDocument();
if( !$rss->loadXML( $contents ) ) 
{
    throw new Exception( 'Impossibile caricare i podcast' );
}

这样,您将加载字符串而不是文件,脚本将可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM