RSS 链接有什么区别?
PHP,阅读 RSS 提要。我的代码工作正常,但不适用于所有 RSS 链接,例如:
制作链接时出错
http://stackoverflow.com/feeds/tag?tagnames=php&sort=newest< /code>
https://www.facebook.com/feeds/page.php?id=20669912712&format=rss20
错误消息
解析错误:语法错误,意外的 T_VARIABLE ...
警告:DOMDocument::load() [domdocument.load]: 开始和结束标记不匹配 ...
这些(上面)RSS 链接存在各种类型的问题我找到的代码。
而且,人们说我这些链接的格式不是“有效”。
但是SimplePie可以很好地阅读这些链接。
- 为什么?
- SimplePie 可以阅读这些链接..而不是我?
我希望我的代码能够在 SimplePie 等所有提要上正常运行..;(
... ...
这是我正在使用的类的简短代码示例:
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 />';
}
}
}
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.
- Why?
- SimplePie can read these links .. and NOT ME?
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 />';
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
许多 RSS 提要实际上是无效的; SimplePie 和其他解析器经常不遗余力地解决这些糟糕的实现,这就是为什么使用这些库比编写自己的库更容易。我们使用 SimplePie,但有大量的 feed 非常糟糕,甚至 SimplePie 也失败了。
简而言之;除非你喜欢痛苦和/或有一些可怕的强迫症来编写自己的库,否则请使用另一个库。正如您所注意到的,尝试将它们解析为纯 XML 常常会失败。
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.