簡體   English   中英

RSS和php解析錯誤

[英]RSS and php parsing error

我希望有人可以在XAMPP上本地解決我的RSS feed問題。

我正在嘗試在我的網站上顯示兩個RSS feed。 一部來自BBC Education的網站,而另一部則來自Joomla網站,該網站不起作用。

除了url以外,我使用的php代碼在兩種情況下都是相同的,也許我必須為每個提要更改我的php嗎? 提要位於同一頁面上,盡管這不重要嗎?

(有效的)BBC提要在這里

我用來成功顯示該提要的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提要在這里

我用於(未成功)顯示此提要的代碼如下;

<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標頭。 導致500錯誤並發出警告的事實(這取決於Web服務器配置,在未設置User-Agent的情況下,並非所有Web服務器都會拋出500錯誤)。

在這種情況下,有兩種添加用戶代理的方法。 第一個基於使用的標准HTTP流上下文選項 ,第二個基於更具體的libxml 選項

第一種方法是更改​​PHP中用戶代理字符串的默認ini設置:

$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);

這已取代了先前對問題“ DOMDocument :: loadHTMLFile()Modify user agent”的回答

第二種方法基於JonasDue Vesterheden對php手冊的評論

提供的解決方案是使用上述標頭創建流,可以通過以下方式實現:

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

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

先前對問題“ DOMDocument :: validate()問題”的回答中也對此進行了概述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM