Combining feeds

A friend suggested I combine my schizophrenic group of blogs into a single blog. I’m not ready to do that but I did decide to display a feed from all my blogs on a single page. Easy enough, but I wanted to combine them all and sort them by date, rather than have a separate feed for each. I found a simple feed puller in a book called Plug-In PHP, by Robin Nixon. From that I modified and expanded the code to allow combining different feeds into one. Also, the books code worked for my wordpress blogs, but then I also wanted to make another feed which included blogs that I read, and my daughter users blogger. I had to write new code to accept her feed. I don’t know if this takes all kinds of feeds, but it does what I’ve needed it to do so far.

<?php
//Sort posts by publication date
function sortpdate ($a, $b) {
 $adate=strtotime($a[‘pubdate’]);
 $bdate=strtotime($b[‘pubdate’]);
 if ($adate == $bdate) { return 0;}
 return ($adate < $bdate) ? 1 : -1;
}

function RSStoHTML ($urllist, $usortfunction, $fullposts=3, $maxposts=12, $titletag=”h4″, $classprefix=””) {
//Takes blog posts from multiple blogs using a comma separated list of feeds, usorts them, and echos them
 

 $urls=explode(‘,’, $urllist);
 $i=0;

 $showitems=array();

 foreach ($urls AS $url) {
  
  $rss=file_get_contents($url);
  $xml = simplexml_load_string($rss);

  //The two foreach loops that follow pickup wordpress and blogger
  //I don’t know yet if these fail on other types of feeds.
  foreach($xml->channel->item as $item) {
   $i++;
   $showitems[$i][‘blogtitle’]=@$xml->channel->title;
   $showitems[$i][‘bloglink’]=@$xml->channel->link;
   $showitems[$i][‘blogdescription’]=@$xml->channel->description;
   $showitems[$i][‘itemlink’] = @$item->link;
   $showitems[$i][‘pubdate’] = @$item->pubDate;
   $showitems[$i][‘itemtitle’] = @$item->title;
   $showitems[$i][‘summary’] = @$item->description;
   $showitems[$i][‘content’] = @$item->children(“content”, true)->encoded;
  }
  foreach($xml->entry as $item) {
   $i++;
   $showitems[$i][‘blogtitle’]=@$xml->title;
   foreach (@$xml->children()->link AS $child ) {
    if($child->attributes()->rel==’alternate’) {
     $showitems[$i][‘bloglink’]=$child->attributes()->href;
    }
   }
   $showitems[$i][‘blogdescription’]=@$xml->subtitle;
   foreach (@$item->children()->link AS $child ) {
    if($child->attributes()->rel==’alternate’) {
     $showitems[$i][‘itemlink’]=$child->attributes()->href;
    }
   }
   $showitems[$i][‘pubdate’] = @$item->published;
   $showitems[$i][‘itemtitle’] = @$item->title;
   $showitems[$i][‘summary’] = substr ( @$item->content , 0, 30 );
   $showitems[$i][‘content’] = @$item->content;
  }
 }
 usort($showitems, $usortfunction);
 $out=””;
 $i=0;
 while ($i < sizeof($showitems) && $i < $maxposts) {
  if($i<$fullposts) {
   echo ‘<‘, $titletag, ‘ class=”‘, $classprefix, ‘itemtitle”><a href=”‘, $showitems[$i][‘itemlink’], ‘”>’, $showitems[$i][‘itemtitle’], ‘</a></’, $titletag, ‘><p class=”‘, $classprefix, ‘pubdate”>’, (date(‘l, Y F d’, strtotime($showitems[$i][‘pubdate’]))), ‘ in <a href=”‘, $showitems[$i][‘bloglink’], ‘”>’, $showitems[$i][‘blogtitle’]>”” ? $showitems[$i][‘blogtitle’] : $showitems[$i][‘bloglink’], ‘</a></p><p class=”‘, $classprefix, ‘summary”>’, $showitems[$i][‘content’], ‘</p>’;
  } else {
   echo ‘<‘, $titletag, ‘ class=”‘, $classprefix, ‘itemtitle”><a href=”‘, $showitems[$i][‘itemlink’], ‘”>’, $showitems[$i][‘itemtitle’], ‘</a></’, $titletag, ‘><p class=”‘, $classprefix, ‘pubdate”>’, (date(‘l, Y F d’, strtotime($showitems[$i][‘pubdate’]))), ‘ in <a href=”‘, $showitems[$i][‘bloglink’], ‘”>’, $showitems[$i][‘blogtitle’]>”” ? $showitems[$i][‘blogtitle’] : $showitems[$i][‘bloglink’], ‘</a></p><p class=”‘, $classprefix, ‘summary”>’, $showitems[$i][‘summary’], ‘</p>’;
  }
  $i++;
 }

 echo $out;
}

$urls=”https://bnmng.wordpress.com/feed,http://bnmngfarming.wordpress.com/feed,http://bnmngcomputing.wordpress.com/feed,http://healthyinsuffolk.wordpress.com,http://sashabyproxy.wordpress.com&#8221;;
RSStoHTML ($urls, ‘sortpdate’);

Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s