使用SimpleXML解析RSS Feed

时间:2011-11-13 11:31:50

标签: simplexml feedparser rss-reader

当我运行此代码时:

<?php
$rss = simplexml_load_file('http://stackoverflow.com/feeds');
?>
<h1><?php echo $rss->title; ?></h1>
<ul>
<?php
foreach($rss->entry as $e) {
    echo "<li><a href=\"".$e->link['href']."\">";
    echo $e->title;     
    echo "</a></li>\n";
}    
?>
</ul>

它会产生以下错误:

Warning: simplexml_load_file(http://stackoverflow.com/feeds) [function.simplexml-load-file]: failed to open stream: Permission denied in /home/www/cheapflightshunt.co.uk/aaaa.php on line 2

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://stackoverflow.com/feeds" in /home/www/cheapflightshunt.co.uk/aaaa.php on line 2

Warning: Invalid argument supplied for foreach() in /home/www/cheapflightshunt.co.uk/aaaa.php on line 7

我的共享主机正在运行PHP5.2.17

请提供解决方案。

提前致谢。

1 个答案:

答案 0 :(得分:1)

您的代码完美无缺。我怀疑您当前的设置不是allow fopen over url

尝试检查。

如果您无法更改该设置,请尝试使用CURL:

<?php
$ch = curl_init('http://stackoverflow.com/feeds');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$rss = new SimpleXMLElement($data);
?>
<h1><?php echo $rss->title; ?></h1>
<ul>
<?php
foreach($rss->entry as $e) {
    echo "<li><a href=\"".$e->link['href']."\">";
    echo $e->title;     
    echo "</a></li>\n";
}    
?>
</ul>
相关问题