Question

I am trying to read data from an RSS feed which has 25 items. When I request the RSS file through HTTP it says there are only 20 items.

function test($location)
{
    $doc = new DomDocument();
    $doc->load($location);
    $items = $doc->getElementsByTagName('item');
    return $items->length;
}

// Prints 20
echo test('http://www.reddit.com/r/programming/new/.rss?after=t3_');

// Prints 25
echo test('programming.xml');

I've tried RSS feeds from other subreddits as well with the same result.

Was it helpful?

Solution

I see what the issue is now... If you visit a sub-reddit like /r/programming/ and go to the "new" tab to see newest submissions, there are two sorting options. The first option is "rising" which only shows up-and-coming entries, the alternate sort order is "new".

Since I chose the "new" sort order in my browser it saved a cookie and was used as the default sort order afterwards. However, accessing the page through code was still using the default sort order, which returned a variable amount of results.

I resolved the issue by appending the sort order query string to the request url: http://www.reddit.com/r/programming/new/.rss?sort=new

OTHER TIPS

If it were having problems loading the feed, it'd probably issue a warning of some sort.

Right now, your sample code for the reddit feed shows that it has 14 items. The number of items in that feed is not constant. So the issue is that your local copy is different that the one you were loading from reddit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top