【问题标题】:SimplePie and fetching multiple feeds from blogspot.comSimplePie 并从 blogspot.com 获取多个提要
【发布时间】:2012-03-09 00:57:42
【问题描述】:

过去几天我一直在使用 SimplePie 做很多工作,我注意到它并不总是对 Blogger 供稿一视同仁。如果我像 http://davetaylorminiatures.blogspot.com/http://sippinonpaintwater.blogspot.com/feeds/posts/default?alt=rss 那样将 Blogger 提要传递给 SimplePie,它会很好地显示它,但是当我传递一组有效提要时,一些来自 blogspot,一些不是来自 blogspot 到 SimplePie,没有任何 blogspot 项目被退回。我让它发现提要或传入指定 RSS 的提要似乎并不重要。

如果 Blogger 博客将 FeedBurner 用于其提要,那么这将起作用,例如此示例 http://feeds.feedburner.com/FromTheWarp SimplePie 将在合并提要中包含来自 blogspot 托管博客的“From the Warp”项目以及来自其他有效提要的数据。

我一直在进行大量的提要合并,起初我认为这与发布日期或本地缓存提要有关,但我已经清除了本地缓存文件并运行了许多测试。

一个 blogspot 提要适用于 SimplePie,传递一组提要和 blogspot 提要似乎被忽略,它们似乎也没有在本地缓存。既然我知道单个提要在提要阅读器甚至 SimplePie News Blocks 2 基于演示的代码中工作,为什么当我将它们传递到数组中时它不起作用?这是 SimplePie 或 Blogger 中的错误,还是我遗漏了一些明显的东西。

我编写了以下代码来测试和演示这种行为,我相信在运行之前不缓存提要很重要。

<?php
require_once('./php/simplepie.inc');

$feed1 = new SimplePie(); // For this test I want four seperate feeds
$feed2 = new SimplePie();
$feed3 = new SimplePie();
$feed4 = new SimplePie();
$feed5 = new SimplePie(); // Fetching the feeds before merging seems to matter with Blogger feeds!

echo "Blogger Feed One http://davetaylorminiatures.blogspot.com/ \n";
echo "---------------------------------------------------------- \n";

$feed1->set_feed_url('http://davetaylorminiatures.blogspot.com/');
$feed1->init();

foreach ($feed1->get_items() as $item)
{
echo $item->get_title();
echo "\n";
}

echo "\n";

echo "Blogger Feed Two http://sippinonpaintwater.blogspot.com/feeds/posts/default?alt=rss \n";
echo "----------------------------------------------------------------------------------- \n";

$feed2->set_feed_url('http://sippinonpaintwater.blogspot.com/feeds/posts/default?alt=rss');
$feed2->init();

foreach ($feed2->get_items() as $item)
{
echo $item->get_title();
echo "\n";
}

echo "\n";

echo "Non-Blogger Feed http://www.witchhunter.net/blog/ \n";
echo "------------------------------------------------- \n";

$feed3->set_feed_url('http://www.witchhunter.net/blog/');
$feed3->init();

foreach ($feed3->get_items() as $item)
{
echo $item->get_title();
echo "\n";
}

echo "\n";

echo "Merged Feeds Test \n";

$feed4->set_feed_url(array('http://davetaylorminiatures.blogspot.com/',
                        'http://sippinonpaintwater.blogspot.com/feeds/posts/default?alt=rss',
                        'http://www.witchhunter.net/blog/'));
$feed4->init();

echo "Merged Feeds Item Titles \n";
echo "------------------------ \n";

foreach ($feed4->get_items() as $item)
{
echo $item->get_title();
echo "\n";
}

echo "\n";

echo "Same Merged Feeds Item and Feed Title \n";
echo "------------------------------------- \n";

foreach ($feed4->get_items() as $item)
{
    echo "\n";
echo $item->get_title();
echo "\n";
echo "From feed: ";
echo $item->get_feed()->get_title();
echo "\n";
}

echo "\n";

echo "Merged Feeds Test, different set of Three Feeds \n";

$feed5->set_feed_url(file('testFeeds.txt'));
$feed5->init();

echo "Merged Feeds Item Titles \n";
echo "------------------------ \n";

foreach ($feed5->get_items() as $item)
{
echo $item->get_title();
echo "\n";
}

echo "\n";

echo "Same Merged Feeds Item and Feed Title \n";
echo "------------------------------------- \n";

foreach ($feed5->get_items() as $item)
{
    echo "\n";
echo $item->get_title();
echo "\n";
echo "From feed: ";
echo $item->get_feed()->get_title();
echo "\n";
}

?>

testFeeds.txt的内容是:

http://cursedtreasures.blogspot.com/
http://sidneyroundwood.blogspot.com/feeds/posts/default?alt=rss
http://feeds.feedburner.com/ChestOfColors

我想看看将提要放在外部文件中是否重要,因为这是我更喜欢获取多个提要的方式。 $feed5 的输出很奇怪,它在第一次运行时只会返回 Chest of Colors 的结果。一旦提要开始被缓存,或者如果您先单独获取提要,这似乎很重要。这是刚才 $feed5 的输出:

合并 Feed 测试,不同的一组三个 Feed

合并的 Feed 项标题

画笔评论:Winsor Newton 系列 7 与 Rosemary and Co NMM 黄金制作快速轻松 5th Chest of Colors 微型交换摘要 Warploque 微型模型:Jebzakkah B'Ork – 回顾本月缩影:2012 年 1 月字里行间 – 第 5 集 Golden Demons 2011:Clash of Slayer Sword 获奖游戏 研讨会:FineCast Jabberslythe – 回顾本月缩影: 2011 年 12 月罗密欧模特:让·巴特 – 回顾

相同的合并 Feed 项和 Feed 标题

画笔评论:温莎牛顿系列 7 vs Rosemary and Co 来自提要: 五彩缤纷 - 所有关于微型绘画

NMM 黄金制作简单快捷 来自 feed:Chest of Colours - All about 微型画

5th Chest of Colors Miniature Exchange 摘要来自 feed:Chest of 颜色 - 关于微型画的一切

Warploque Miniatures:Jebzakkah B'Ork – 来自提要的评论:Chest of 颜色 - 关于微型画的一切

本月缩影:2012 年 1 月来自提要:Chest of Colours - All 关于微缩画

字里行间 - 第 5 集来自提要:Chest of Colours - All about 微型画

Golden Demons 2011:Clash of Slayer Sword 获胜者来自 feed:Chest of 颜色 - 关于微型画的一切

游戏工作室:FineCast Jabberslythe – 来自 feed 的评论:Chest of 颜色 - 关于微型画的一切

当月缩影:2011 年 12 月来自提要:Chest of Colours - All 关于微缩画

Romeo 模特:Jean Bart – 来自 feed 的评论:Chest of Colours - All 关于微缩画

除了在尝试合并之前您是否已经单独获取并缓存了 Blogger 提要这一显而易见的事情之外,您还有什么想法吗?测试脚本已启动并运行here

【问题讨论】:

  • 我仍然不知道为什么,但我写了一个更简单的测试用例link 它需要五个 SimplePie 实例,也许不需要,我使用了来自 Blogger 的四个提要,两个来自其他地方的提要。吹走缓存的数据很重要。如果您单独获取提要并显示标题,然后合并它会按预期工作,但如果您不单独获取提要并考虑第二组三个提要,其中两个来自 Blogger,一个不是,只有不来自 Blogger 的项目出现在合并的结果中。吹走缓存很重要。我会尝试在上面添加我的测试代码。
  • 调用 $item->get_feed()->get_title() 可能会强制获取 Blogger 提要,但在第五个测试用例中,这还不够。这就是为什么我循环通过合并的提要 $feed4 和 $feed5 两次。这种行为真的让我很烦,我可以用测试用例记录它很好,但这对我目前使用 SimplePie 的项目很不利......
  • blogspot 会限制我吗?它在早上开始工作是因为我当天还没有受到限制吗?我在新闻阅读器 (NewsFire) 中订阅的这些提要,但它可能会从不同的域/代理中提取数据...
  • 我很确定尝试了很多设置组合,但 init() 出了点问题,但我也发现问题不仅仅是 blogspot 提要,我尝试过使用自动发现,但我还发现,大多数 blogspot 博客(如corbaniaprime.blogspot.com)都有使用 Feedburner 的 Atom 提要,例如 feeds.feedburner.com/CorbaniaPrime 吹走缓存并使用 Feedburner 提要重新运行......但我必须手动检查和更改每个提要。想法?
  • 我想出了一些解决方法,但我仍然认为在 SimplePie 和 blogspot 提要的 init() 中可能存在问题。基本上,我获取一个随机提要并将其显示为 news.muschamp.ca 的一部分,然后将一堆提要与其他一些 PHP 和 SimplePie 合并并显示合并的提要。随着单个提要被缓存,合并的提要逐渐变大。我仍然希望在将 blogspot 提要与 SimplePie 合并时不会出现这个问题,就像我上面演示的那样......

标签: rss feed blogger simplepie blogspot


【解决方案1】:

我正在使用 SimplePie 1.3(最新版本),并以这种方式编写代码,它似乎对我有用:

$feed = new SimplePie();

$feed_ary = array();
$feed_ary[] = 'http://simplepie.org/blog/feed/';
$feed_ary[] = 'http://hurtnordic.blogspot.com/feeds/posts/default?alt=rss';
$feed_ary[] = 'http://rochesternordicracing.blogspot.com/feeds/posts/default?alt=rss';

// Set the feed(s) to process. Blogspot format: http://blogname.blogspot.com/feeds/posts/default?alt=rss
$feed->set_feed_url($feed_ary);

// limit the number of items
$feed->set_item_limit($max_items_per_feed);

// Run SimplePie.
$success = $feed->init();

$feed->handle_content_type();

它合并提要并按日期对其进行排序,因为列出的所有提要都有日期字段。请注意,blogspot 提要的格式为:Blogspot 格式:http://blogname.blogspot.com/feeds/posts/default?alt=rss,您的部分提要 URL 中都有此格式。

【讨论】:

  • 感谢您的尝试,但我放弃了这个并编写了一个解决方法。我会接受你的回答,也许是 SimplePie 本身现在修复的任何错误。
  • 感谢您接受我的回答,至少您​​有一个解决方法。如果您确实获得了有关此的更多信息,请将其发布以供后代使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多