PHP中的RSS阅读器,有些问题

时间:2014-03-21 21:19:51

标签: php rss

一开始抱歉我的语言,但英语不是我的母语:)

所以,现在我正在为我的网站做一个RSS阅读器。但在此期间,我遇到了一些问题。

1)如何按时间顺序显示新闻?我的意思是现在我已经混合了所有内容 - 最新消息可以在底部,最旧的消息可以在列的顶部。

2)如何设置显示新闻的限制?例如,我想只显示10个项目。

3)如何显示某些新闻的来源?我的意思是现在我只有头衔和日期,但我想向其他人展示那个新闻的网站。

提前致谢!问候!

显示RSS新闻:

`

<?php include "feeds.php"; 

$urls_pl = array('http://feeds.feedburner.com/sportowefakty/TtDh','http://www.goal.pl/rss.php');

$urls = array('http://football-italia.net/rss.xml','http://www.fifa.com/rss/index.xml');

try
{
$feeds = new Feed_Amalgamator;
$feeds->addFeeds( $urls );
$feeds->grabRss();
$feeds->amalgamate();
$feeds_pl = new Feed_Amalgamator;
$feeds_pl->addFeeds( $urls_pl );
$feeds_pl->grabRss();
$feeds_pl->amalgamate();
}
catch ( exception $e )
{
die( $e->getMessage() );
}
?> 
<div style="border: 1px solid black; background-color: #CCCCCC; padding: 5px;">
<a id="myHeader1" href="javascript:showonlyone('newboxes1');" ><img src="http://www.ligamistrzow.com/img/kraje/1.png" height="10px"> POLSKA </a>
</div>

<div style="border: 1px solid black; background-color: #CCCCCC; padding: 5px;">
<a id="myHeader2" href="javascript:showonlyone('newboxes2');" ><img src="http://www.mricons.com/store/png/114777_32086_64_explorer_globe_internet_icon.png" height="15px"> ZAGRANICA</a>
</div>
<div class="newboxes" id="newboxes1" style="border: 1px solid black; background-color: white; display: none;padding: 5px;">
<?php foreach ( $feeds_pl->data as $item ) : extract( (array) $item);?>
<li><a href="<?php echo $link; ?>" target="_blank"><?php echo $title; ?></a> <font size="1"><?php echo $pubDate ?></font></br>
<?php endforeach; ?></div>
<div class="newboxes" id="newboxes2" style="border: 1px solid black; background-color: white; display: none;padding: 5px;">
<?php foreach ( $feeds->data as $item) : extract( (array) $item);?>
<li><a href="<?php echo $link; ?>" target="_blank"><?php echo $title; ?></a> <font size="1"><?php echo $pubDate ?></font></br>
<?php endforeach; ?></div>
</div>

<!-- KONIEC -->`

FEEDS.PHP文件

  <script type="text/javascript">
function showonlyone(thechosenone) {
    var newboxes = document.getElementsByTagName("div");
    for(var x=0; x<newboxes.length; x++) {
        name = newboxes[x].getAttribute("class");
        if (name == 'newboxes') {
            if (newboxes[x].id == thechosenone) {
                newboxes[x].style.display = 'block';
            }
            else {
                newboxes[x].style.display = 'none';
            }
        }
    }
}
</script>

<?php class Feed_Amalgamator
{
public $urls = array();
public $data = array();

public function addFeeds( array $feeds )
{
    $this->urls = array_merge( $this->urls, array_values($feeds) );
}

public function grabRss()
{
    foreach ( $this->urls as $feed )
    {
        $data = @new SimpleXMLElement( $feed, 0, true );
        if ( !$data )
        throw new Exception( 'Could not load: ' . $feed );
        foreach ( $data->channel->item as $item )
        {
            $this->data[] = $item;
        }
    }
}

public function amalgamate()
{
    shuffle( $this->data );
    $temp = array();
    foreach ( $this->data as $item )
        {
            if ( !in_array($item->link, $this->links($temp)) )
            {
                $temp[] = $item;
            }
    }
    $this->data = $temp;
    shuffle( $this->data );
}

private function links( array $items )
{
    $links = array();
    foreach ( $items as $item )
    {
        $links[] = $item->link;
    }
    return $links;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

simplepie feed解析器是你最好的朋友:

http://simplepie.org/