RSS feed url返回空

时间:2014-03-26 14:58:38

标签: php xml wordpress rss feed

我正在尝试从Wordpress .php页面中的外部xml文件呈现rss提要:

    <?php
    $rss = new DOMDocument();
    $rss->load('http://feeds.bbci.co.uk/news/rss.xml?edition=uk');
    $feed = array();

    foreach ($rss->getElementsByTagName('item') as $node) {

        $item = array (
            'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue,
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->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'];
        $date = date('l F d, Y', strtotime($feed[$x]['date']));
        echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
        echo '<small><em>Posted on '.$date.'</em></small></p>';
        echo '<p>'.$description.'</p>';
    }
   ?>

感谢Bavotasan的文章“用PHP显示RSS源的简单方法”,这完全适用于所显示的BBC网址。但是当我将BBC网址更改为可以在浏览器中查看而没有错误的外部(公共)网址时,它不起作用。即使是在“foreach”语句之前的$ rss的var_dump也会返回:

    object(DOMDocument)#51 (34) { ["doctype"]=> NULL ["implementation"]=> string(22) "(object value omitted)" ["documentElement"]=> NULL ["actualEncoding"]=> NULL ["encoding"]=> NULL ["xmlEncoding"]=> NULL ["standalone"]=> bool(true) ["xmlStandalone"]=> bool(true) ["version"]=> string(3) "1.0" ["xmlVersion"]=> string(3) "1.0" ["strictErrorChecking"]=> bool(true) ["documentURI"]=> NULL ["config"]=> NULL ["formatOutput"]=> bool(false) ["validateOnParse"]=> bool(false) ["resolveExternals"]=> bool(false) ["preserveWhiteSpace"]=> bool(true) ["recover"]=> bool(false) ["substituteEntities"]=> bool(false) ["nodeName"]=> string(9) "#document" ["nodeValue"]=> NULL ["nodeType"]=> int(9) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> NULL ["attributes"]=> NULL ["ownerDocument"]=> NULL ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> NULL ["baseURI"]=> NULL ["textContent"]=> string(0) "" }

为了大肆无益,我不能自由地引用有问题的网址,但我希望只有少数情况下,使用此方法无法将在浏览器中呈现正常的有效网址分配给变量。

打开bbc源代码行:(工作):

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/shared/bsp/xsl/rss/nolsol.xsl"?>

    <rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">  

打开所需源代码行(不工作):

    <?xml version="1.0" encoding="utf-8" ?>
    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:tp="http://www.xxxxxxxx.co.uk/rss/">

各自的XML结构几乎相同并且有一个共同点:

    <channel><title>
    <channel><link>
    <channel><description>

...等。我的问题是,为什么违规网址不起作用?提前谢谢......

注意,我对php / xml知识的处理程度相当高,所以非常了解外行人的术语和lego-brick简单的解释。

更新

最后成功 - 同事试图查看Feed url是否已压缩 - 事实确实如此。所以更新的代码是:

    function download_page($path){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$path);
    curl_setopt($ch, CURLOPT_FAILONERROR,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch,CURLOPT_ENCODING , "gzip");
    $retValue = curl_exec($ch);
    curl_close($ch);
    return $retValue;
}

$foo = download_page('http://xxxxxxxx.xml');
$rss = new DOMDocument();
$rss->loadXML($foo);

$feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {

    $item = array (
        'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue,
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->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'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
    echo '<small><em>Posted on '.$date.'</em></small></p>';
    echo '<p>'.$description.'</p>';
}

感谢您的帮助......

0 个答案:

没有答案