关于显示来自两个或多个博客的帖子

时间:2012-07-07 04:19:32

标签: php blogs

我想在我的网站上显示两个或更多博客的帖子,现在使用 magpierss-0.72 来获取帖子,我的代码是

require_once('rss_fetch.inc');
    $url = 'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss'
    /*'http://raghuks.wordpress.com/feed/'*/;
    $rss = fetch_rss($url);
    foreach ($rss->items as $i => $item ) { 
        $title = strtoupper ($item['title']);
        $url   = $item['link'];
        $date = substr($item['pubdate'],0,26);
        //code to fetch only some text
        $desc = '';
        $max = 30;
        $arr = explode(' ', strip_tags($item['description']));
        $l = count($arr);
        if($l < $max) $max = $l;
        for($j=0;$j<$max;++$j) {
            $desc .= $arr[$j] . ' ';
        }
        $desc .= '.....';
        echo "<div class=\"blog\"><a target=\"_blank\" href=$url><h1>$title</h1>$desc<br/><br/>DATED : $date <br/><br/></a></div> ";
        if($i == 3) break; 
    }

在这里我只能指定一个Feed的网址并且可以获取,但现在我想显示两个或更多博客的帖子请给我解决方案

提前致谢

1 个答案:

答案 0 :(得分:1)

只需使用数组并引入另一个foreach:

<?php
require_once('rss_fetch.inc');

$urls = array(
'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss',
' more urls ... ',
);

foreach($urls as $url) {
    /*'http://raghuks.wordpress.com/feed/'*/;
    $rss = fetch_rss($url);
    foreach ($rss->items as $i => $item ) { 
        $title = strtoupper ($item['title']);
        $url   = $item['link'];
        $date = substr($item['pubdate'],0,26);

        //code to fetch only some text
        $desc = '';
        $max = 30;
        $arr = explode(' ', strip_tags($item['description']));
        $l = count($arr);
        if($l < $max) $max = $l;
        for($j=0;$j<$max;++$j)
        {
            $desc .= $arr[$j] . ' ';
        }
        $desc .= '.....';
        echo "<div class=\"blog\"><a target=\"_blank\" href=$url><h1>$title</h1>$desc<br/><br/>DATED : $date <br/><br/></a></div> ";
        if($i == 3) break; 
    }
}