RSS 和 php 解析错误

RSS and php parsing error

我希望有人可以帮助我解决我在本地工作时遇到的 RSS 提要问题,使用 XAMPP。

我正在尝试在我的网站上显示两个 RSS 提要。一个来自 BBC Education 有效,另一个来自 Joomla 站点,但无效。

我使用的 php 代码在这两种情况下 相同 除了 url,也许我必须为每个 Feed 改变我的 php 某种方式吗?提要在同一页面上,尽管这无关紧要?

(有效的)BBC 提要是 here

我用来成功显示此提要的 php 代码如下;

<div class="panel-body">
    <?php
    $rss = new DOMDocument();
    $rss->load('http://feeds.bbci.co.uk/news/education/rss.xml?edition=uk');
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
     'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
     'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
     'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
     );
    array_push($feed, $item);
    } 
    $limit = 3;
    for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
    echo '<p>'.$description.'</p>';
    }  
    ?>
    </br>
    <a href="#" class="btn btn-default">Learn More</a>
</div>

(不工作)Joomla 提要是 here

我用来(未成功)显示此提要的代码如下;

<div class="panel-body">
    <?php
    $rss = new DOMDocument();
    $rss->load('http://www.littlehandssurestart.co.uk/blog?format=feed&type=rss');
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
     'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
     'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
     'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
     );
    array_push($feed, $item);
    } 
    $limit = 3;
    for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
    echo '<p>'.$description.'</p>';
    }  
    ?>
    </br>
    <a href="#" class="btn btn-default">Learn More</a>
</div>

收到关于上述 Joomla 提要的错误如下;

Warning: DOMDocument::load(): Empty string supplied as input in C:\xampp\htdocs\logintest\application\views\index\index.php on line 124

Notice: Undefined offset: 0 in C:\xampp\htdocs\logintest\application\views\index\index.php on line 136

Notice: Undefined offset: 0 in C:\xampp\htdocs\logintest\application\views\index\index.php on line 137

Notice: Undefined offset: 0 in C:\xampp\htdocs\logintest\application\views\index\index.php on line 138

我听说这可能是由于 /> 标签未关闭造成的?但是我找不到任何。对 php 很陌生,因此不胜感激。

幸运的是,DOMDocumentload() 函数不会发送 User-Agent header。导致 500 错误并发出警告的事实(这取决于网络服务器配置,并非所有网络服务器都会抛出 500 错误以防 User-Agent 未设置)。

在这种情况下,有两种方法可以添加 user-agent。第一个是基于使用的标准 HTTP stream context options and the second one on the more specific libxml ones.

第一种方法是更改​​ PHP 中 user-agent 字符串的默认值 ini-setting:

$fake_user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11";
ini_set('user_agent', $fake_user_agent);

这已经接替了 previous answer to the question "DOMDocument::loadHTMLFile() modify user agent"

第二种方法基于 php 手册中的 JonasDue Vesterheden's comment

提供的解决方案是使用提到的 header 创建一个流,可以通过以下方式实现:

$opts = array(
    'http' => array(
        'user_agent' => 'PHP libxml agent',
    )
);

$context = stream_context_create($opts);
libxml_set_streams_context($context);

previous answer to the question "DOMDocument::validate() problem" 中也对此进行了概述。