Aggregating Your Social Bookmarking Feeds With SimplePie

Sep. 9, 2008

1:38 pm

Now that Agregado is launched, I'm starting to review what I learned during the build process. In short: tons. But how about an example?

The wonders of SimplePie.

Agregado uses SimplePie to aggregate RSS feeds from services you use anywhere on the web. When you call SimplePie in your program, you first instantiate a new SimplePie object. For all intents and purposes, what you've got is a single feed entity that you can print to your page, traverse, and otherwise mangle with code. That's great news for lifestream fans because it abstracts the parts of the task that are really just mind-numbingly boring – that is, ordering your items by date, caching, and the like. It's a real time-saver for developers. One of the reasons SimplePie is gaining popularity is how dead simple it is to instantiate one of these objects in your code:

  1. $feed = new SimplePie(array(
  2. '',
  3. ''
  4. ), $_SERVER['DOCUMENT_ROOT'] . '/cache');

That's all it takes to set up an object that contains items from any assortment of arbitrary feeds.

The problem.

The trouble starts when you want to get information about the source of one of those feeds you just mashed together. To SimplePie, where the feeds came from doesn't matter so much anymore. But if you're building a lifestream for your site, you'll likely want to assign classes to each item – perhaps to give them a particular color or place an icon next to the link. There are a couple ways you can tackle the problem. You could use PHP to look at the content of the link and assign a class that way. But what about social bookmarking sites like Delicious or Magnolia? In those cases, the feed link will point to the site you've bookmarked, not the service you used. And if you've bookmarked a site on Delicious, you'll probably want to indicate that on your lifestream.

A solution.

Fortunately, there's an easy way around this. All you've gotta do is iterate over the items in your master feed, and for each item "climb out" of the master feed using SimplePie's get_feed() method. At it's simplest (hyuck, hyuck), the code would look something like this:

  1. <?php
  2. foreach ($feed->get_items() as $item)
  3. {
  4. $parent_feed = $item->get_feed();
  5. $address = $parent_feed->subscribe_url();
  6. if (strstr($address, 'thenestedfloat'))
  7. {
  8. $class = "myblog";
  9. }
  10. elseif (strstr($address, 'delicious')) {
  11. $class = "delicious";
  12. }
  13. ?>
  14. <li class="<?php echo $class; ?>"><?php echo $item->get_title(); ?></li>
  15. <?php
  16. }
  17. ?>

Take note of lines 4 and 5. That's where we're getting the address of the parent feed for the link. After that, all we're doing is printing out the item and using the PHP strstr function to see which service we're working with.

Isn't that nice? Yeah. All the way nice.


January 10, 2010

11:38 pm

An social book marking should give god promotion and page rank for the web site and development and I would welcoming the social bookmarks for internet marketing too..

Internet marketing company (#)

March 31, 2010

11:41 pm

Social bookmarking feeds is good, i think. And that article is amazing! Very thanks.

True Blood (#)

April 16, 2010

10:20 am

you’ve been separate of my “indirect mentor” successful a lot of directions - it’s existing a pleasure learned you complete the previous different years and I looking forward to seeing where life takes you next! Good vacations & good new twelvemonth! When’s the sister due?
montreal incalls

Adley (#)

April 28, 2010

1:08 pm

It just means that people are adding more feeds to Google Reader (which makes .... Never thought that Google is big in social bookmarking.

dmoz site submission (#)

Whaddya think?