anipaul
anipaul

Reputation: 11

Rss feed not working in PHP

In my webpage everything works except the rss feed. All the html code and scripts are loaded. But the rss feed is blank. I tried different formats but none is working. Please help. I used the code in seperate file as functions.php and called it in index.php

functions.php

<?php

function parserSide($feedURL) {
    $rss = simplexml_load_file($feedURL);
    echo "<ul class='newsSide'>";
    $i = 0;
    foreach ($rss->channel->item as $feedItem) {
        $i++;
        echo "<li><a href='$feedItem->link' title='$feedItem->title'>" . $feedItem->title . "</a></li>";
        if($i >= 5) break;
    }
    echo "</ul>";
}

index.php

<?php

require_once('functions.php');
parserSide("http://feeds.reuters.com/reuters/technologyNews"); ?>

Upvotes: 1

Views: 835

Answers (2)

Julian Kolev
Julian Kolev

Reputation: 123

Can't see any problems, except that you're not checking the return value of simplexml_load_file. On failure, the function will return FALSE and most likely that is the case. Or, remote file access is disabled for your server as in here: simplexml_load_file not working?

Upvotes: 1

Bora
Bora

Reputation: 10717

  1. Use return instead of echo in function
  2. Make sure functions.php file included

functions.php

function parserSide($feedURL) {
    $rss = simplexml_load_file($feedURL);
    $output = "<ul class='newsSide'>";
    $i = 0;
    foreach ($rss->channel->item as $feedItem) {
        $i++;
        $output .= "<li><a href='$feedItem->link' title='$feedItem->title'>" . $feedItem->title . "</a></li>";
        if($i >= 5) break;
    }
    $output .= "</ul>";

    return $output;
}

index.php

require_once('functions.php');

echo parserSide("http://feeds.reuters.com/reuters/technologyNews");

Upvotes: 0

Related Questions