使用 PHP 显示多个 RSS 提要 [英] Displaying Multiple RSS Feeds with PHP

查看:45
本文介绍了使用 PHP 显示多个 RSS 提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道怎么做吗?我知道如何将单个提要显示为一个简单的列表,但它变得更加复杂.我想显示一个列表但有多个来源,每个来源都有 list-style-image 是特定的.

Does anyone know how to do this? I know how to display a single feed as a simple list, but it gets more complicated. I want to display a list but have multiple sources, with each source having the list-style-image be specific.

示例:

(img1) This is data from rss feed 1
(img2) This is data from rss feed 2

非常感谢您的帮助!

这就是我对单一来源列表的看法.

This is what I have for a single source list.

# INSTANTIATE CURL.
$curl = curl_init();

# CURL SETTINGS.
curl_setopt($curl, CURLOPT_URL, "RSSURL");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);

# GRAB THE XML FILE.
$xmlTwitter = curl_exec($curl);

curl_close($curl);

# SET UP XML OBJECT.
$xmlObjTwitter = simplexml_load_string( $xmlTwitter );

$tempCounter = 0;

foreach ( $xmlObjTwitter->channel->item as $item )
{                    
    # DISPLAY ONLY 10 ITEMS.
    if ( $tempCounter < 11 )
    {
        echo "<li><a href=\"{$item -> guid}\">{$item -> title}</a></li>
";
    }

    $tempCounter += 1;
}

编辑 1

好的,非常感谢.非常有帮助.我试图通过以下方式实现它:1. 将 SimplePie 安装到我网站根目录的 PHP 文件夹中.2. 实现给定的代码.(还没有样式,想先让它发挥作用.)

Alright, Thanks a lot. Very helpful. I've attempted to implement it by: 1. Installing SimplePie into the root of my site in the PHP folder. 2. Implenting the given code. (No styling yet, want to get it functional first.)

我目前的代码是这样的:

My code currently is this:

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

$feed = new SimplePie();
$feed->set_feed_url(array('rss1', 'rss2'));
$feed->init();
$feed->handle_content_type();
foreach ($feed->get_items(0, 100) as $item) {
    $n = array_search($item->get_feed(), $feeds);
    echo '<li class="feed'.$n.'">'.$item->get_title().'</li>';
}
?>\

我在我的网站上得到这个输出:

I'm getting this output on my site:

set_feed_url(array('http://vimeo.com/user/likes/rss','http://vimeo.com/user/videos/rss'));$feed->init();$feed->handle_content_type();foreach($feed->get_items(0, 100) 作为 $item) {$n = array_search($item->get_feed(),$饲料);echo ' '.$item->get_title().'';} ?>\

set_feed_url(array('http://vimeo.com/user/likes/rss', 'http://vimeo.com/user/videos/rss')); $feed->init(); $feed->handle_content_type(); foreach ($feed->get_items(0, 100) as $item) { $n = array_search($item->get_feed(), $feeds); echo ' '.$item->get_title().' '; } ?>\

我显然做错了什么,但我很难发现它是什么.再次感谢.

I'm obviously doing something wrong, but am having trouble discovering what it is. Thanks again.

推荐答案

您似乎有两个问题:聚合多个 RSS 提要,然后将它们显示在列表中.

It seems that you have two problems: aggregating multiple RSS feeds and then displaying them in a list.

1:使用 SimplePie 库——对于与 RSS 相关的任何东西来说,这可能是最简单和最好的库.由于文件大小,对某人来说可能看起来有点矫枉过正,但无论如何.它还将为您处理按日期排序.http://simplepie.org/

1: Use SimplePie lib -- probably the simplest and best one for anything related to RSS. Might seem as an overkill for someone because of the file size, but anyway. It will also handle sorting by date for you. http://simplepie.org/

<?php
$feed = new SimplePie();
$feed->set_feed_url(array('http://rss1', 'http://rss2'));
$feed->init();
$feed->handle_content_type();
foreach($feed->get_items(0, 100) as $item) {
    $n = array_search($item->get_feed(), $feeds);
    echo '<li class="feed'.$n.'">'.$item->get_title().'</li>';
}
?>

2:akamike 说了什么(并删除了?),但是列表样式图像可能很痛苦——几乎不可能对齐.更灵活的解决方案是删除它们并使用背景.

2: What akamike said (and deleted?), but list style images can be a pain -- almost impossible to align. A more flexible solution would be to remove them and use background.

CSS:

li.feed1, li.feed2 {
    list-style: none;
    background-position: left center;
    background-repeat: no-repeat; 
}

li.feed1 { background-image: url(feed1icon.png); }
li.feed2 { background-image: url(feed2icon.png); }

这篇关于使用 PHP 显示多个 RSS 提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆