PHP RSS获取并显示多个RSS频道的标题
我有以下代码:PHP RSS获取并显示多个RSS频道的标题,php,codeigniter,rss,Php,Codeigniter,Rss,我有以下代码: $rss[] = $this->rssparser->set_feed_url('someUrl')->set_cache_life(30)->getFeed(4); $rss[] = $this->rssparser->set_feed_url('someURL')->set_cache_life(30)->getFeed(3); $rss[] = $this->rsspars
$rss[] = $this->rssparser->set_feed_url('someUrl')->set_cache_life(30)->getFeed(4);
$rss[] = $this->rssparser->set_feed_url('someURL')->set_cache_life(30)->getFeed(3);
$rss[] = $this->rssparser->set_feed_url('someURL')->set_cache_life(30)->getFeed(5);
它们中的每一个都显示在不同的div中,我想要的是相关频道的标题显示在相关div中
到目前为止,我已经:
foreach ($rss as $feed)
{
$channel = $this->rssparser->channel_data['title'];
$result = "<div class='rssFeed'>";
$result .= '<h3>'.$channel.'</h3>';
foreach ($feed as $item)
{
$result .= '<div class="rssContent">
<a href="'.$item['link'].'">'.$item['title'].'</a>
<br />
<span>'.$item['pubDate'].'</span>
</div>
';
}
$result .= '</div>';
echo $result;
}
但这只会在顶部显示数组最后一个通道的标题
有人知道我做错了什么吗?您可以在获取下一个提要之前存储频道标题。例如:-
<?php
class Controller
{
public function action()
{
$feeds = array(
array('url' => 'http://example.org/feed.rss', 'count' => 3),
array('url' => 'http://example.org/feed.rss', 'count' => 4),
array('url' => 'http://example.org/feed.rss', 'count' => 5),
);
$rss = array();
foreach ($feeds as $feed) {
$rss[] = array(
'items' => $this->rssparser->set_feed_url($feed['url'])->set_cache_life(30)->getFeed($feed['count']),
'title' => $this->rssparser->channel_data['title'],
);
$this->rssparser->clear();
}
}
}
然后在提要上进行如下迭代:-
<?php foreach ($rss as $feed): ?>
<h1><?php echo $feed['title']; ?></h1>
<?php foreach ($feed['items'] as $item): ?>
<a href="<?php echo $item['link']; ?>">
<?php echo $item['title']; ?>
</a>
<?php endforeach; ?>
<?php endforeach; ?>