使用Simple Pie解析多个Feed而不合并它们

时间:2013-12-03 04:10:34

标签: php rss simplepie

我正在使用Simplepie从数据库表中获取rss提要。我试图在他们各自的div(例如一个或多个不同的列)上显示这些Feed,其各自的项目数也来自DB。我有效地将每个Feed视为SimplePie的单独实例。

我的问题是,我可以实例化与Simplepie对象一样多的实例,因为db中有rss feed(参见下面的概念证明代码)。这对我来说可以手动使用两个源,但是我想知道用户是否有50个源可以继续实例化这样的SimplePie对象还是有更好的方法?

我担心可扩展性和正确性。

这就是我目前正在做的事情:

require_once('../php/autoloader.php');

// We'll process this feed with all of the default options.
$feed = new SimplePie();
$feed2 = new SimplePie();
// Set which feed to process.
$feed->set_feed_url('http://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml');
$feed2->set_feed_url('http://www.synthtopia.com/feed/');
// Run SimplePie.
$feed->init();
$feed2->init();

// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set
$feed->handle_content_type();
$feed2->handle_content_type();


// Then display feeds 
...

我的另一个问题是,如何以编程方式创建更多变量$feed3, $feed4, $feed5' etc without having to write them manually. Do I just do a forerch and append {$i} to feeds like so? $ {“feed”。 $ i}`或者有更好的方法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

你的方法基本上很好IMO虽然如果每个用户实例化50个对象,你可能会看到一些慢的页面加载时间。

我没有实例化单独的变量,而是使用数字索引数组,只需按照您希望它们的顺序从数据库中返回Feed url

$feeds = array();
// get feeds from db
$feeds[1] = $url_1; // first url from db query
...
$feed->set_feed_url($feeds[1]);
// etc.