PHP, reading RSS Feeds. My codes works fine but NOT for all RSS links, for example:
Error Making Links
http://stackoverflow.com/feeds/tag?tagnames=php&sort=newest
https://www.facebook.com/feeds/page.php?id=20669912712&format=rss20
Error Messages
Parse error: syntax error, unexpected T_VARIABLE ...
Warning: DOMDocument::load() [domdocument.load]: Opening and ending tag mismatch ...
These (above) RSS links are problem with various type of codes i found.
And, people saying me these links are not the "valid" formatted.
But SimplePie can read these links well.
I want my codes to work well on all feeds like SimplePie.. ;(
... ...
Here is brief code sample of class i'm using:
class RSSREADER {
private $xml;
private $items;
private $item;
public static function _ParseFeeds ($link)
{
$self->xml = @simplexml_load_file($link);
echo $self->xml->channel->title.'<br />';
echo $self->xml->channel->description.'<br /><br />';
$self->items = $self->xml->channel->item;
foreach ($self->items as $self->item) {
echo $self->item->title.'<br />';
echo $self->item->description.'<br />';
}
}
}
Plenty of RSS feeds are actually invalid; SimplePie and other parsers often go to great lengths to work around these terrible implementations, which is why it's easier to use these libraries than to write your own. We use SimplePie and there are plenty of feeds out there that are so terrible that even SimplePie fails.
In short; unless you like pain and/or have some terrible driving compulsion to write your own, use another library. As you've noticed, trying to parse them as pure XML will fail extremely often.