【问题标题】:Displaying Multiple RSS Feeds with PHP使用 PHP 显示多个 RSS 提要
【发布时间】:2010-05-09 22:18:57
【问题描述】:

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

例子:

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

非常感谢您的帮助!

这就是我所拥有的单一来源列表。

# 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. 实现给定的代码。 (还没有样式,想先让它发挥作用。)

我目前的代码是这样的:

<?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>';
}
?>\

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

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

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

【问题讨论】:

  • 贴一点代码。我认为这更像是一个 CSS 而不是 PHP 问题。
  • 你能给你的
  • 元素添加一个样式吗?
  • 抱歉什么意思?编辑代码以显示一些样式?
  • 标签: php rss


    【解决方案1】:

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

    1:使用 SimplePie 库——对于与 RSS 相关的任何事情,这可能是最简单和最好的库。由于文件大小的原因,对某人来说可能看起来有点矫枉过正,但无论如何。它还将为您处理按日期排序。 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 说了什么(并删除了?),但列表样式的图像可能会很痛苦——几乎不可能对齐。更灵活的解决方案是删除它们并使用背景。

    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); }
    

    【讨论】:

    • 知道为什么我会在我的网站上获得该输出吗?
    • require 路径 ('/simplepie.inc') 看起来很可疑——如果您没有修改 include_path,它可能会查看系统根目录。尝试使用像“../simplepie.inc”这样的相对路径或像“/home/user/www/site/simplepie.inc”这样的站点的绝对路径。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签