RSS feed url returning empty

I’m trying to render an rss feed from an external xml file in a WordPress .php page:

    <?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>';
    }
   ?>

Thanks to Bavotasan’s article called “An Easy Way to Display an RSS Feed with PHP”, this works PERFECTLY with the BBC url shown. BUT when I change the BBC url to an external (public) url that CAN be viewed in a browser with no errors, it doesn’t work. Even a var_dump of $rss immediately before the “foreach” statement returns this:

Read More
    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) "" }

To be massively unhelpful I’m not at liberty to refer to the offending url, but am hoping there can only be a handful of instances where a valid url that renders fine in a browser cannot be assigned to a variable using this method.

opening lines of bbc source code: (working):

    <?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">  

opening lines of desired source code (NOT working):

    <?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/">

The respective XML structures are almost identical and share a common:

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

…etc. My question is why won’t the offending url work? Thanks in advance…

NB I’m at a fairly infant level of php / xml knowledge, so laymen’s terms and lego-brick simple explanations are appreciated.

update

Success at last – colleague tried to see if feed url was zipped – and it was. So updated code is:

    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>';
}

Thanks for all help…

Related posts

Leave a Reply