0

I am hopeing somebody can help me out with an RSS feed issue I am having, working locally, using XAMPP.

I am trying to display two RSS feeds on my website. One from BBC Education which works, and one from a Joomla site, which doesn't work.

The php code I am using is identical in both cases apart from the url, perhaps I have to change my php some way for each feed? The feeds are on the same page, although this shouldn't matter?

The (working) BBC feed is here

The php code I use to successfully display this feed is below;

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

The (not working) Joomla feed is here

The code I use to (unsuccessfully) display this feed is below;

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

The error is receive regarding the Joomla feed above is as follows;

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

I have read that this can be due to unclosed /> tags? However I can't find any. Quite new to php so any help is appreciated.

1

1 Answer 1

2

Luckily apparently, the load() function of DOMDocument doesn't send a User-Agent header. That fact causing a 500 error and a warning is being raised (this depends on webserver configuration, not all webservers throw a 500 error in case the User-Agent is not set).

There are two ways to add a user-agent in this case. The first one is based on the used standard HTTP stream context options and the second one on the more specific libxml ones.

The first method is to change the default ini-setting in PHP for the user-agent string:

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

This has been taking over from a previous answer to the question "DOMDocument::loadHTMLFile() modify user agent".

A second method is based on JonasDue Vesterheden's comment on the php manual.

The offered solution is to create a stream with the mentioned headers, which can be achieved by the following:

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

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

This has also been outlined in a previous answer to the question "DOMDocument::validate() problem".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.