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:
- $feed = new SimplePie(array(
- 'http://simplepie.org/blog/feed/',
- 'http://digg.com'
- ), $_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:
- <?php
- foreach ($feed->get_items() as $item)
- {
- $parent_feed = $item->get_feed();
- $address = $parent_feed->subscribe_url();
- if (strstr($address, 'thenestedfloat'))
- {
- $class = "myblog";
- }
- elseif (strstr($address, 'delicious')) {
- $class = "delicious";
- }
- ?>
- <li class="<?php echo $class; ?>"><?php echo $item->get_title(); ?></li>
- <?php
- }
- ?>
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.






Comments
January 10, 2010
11:38 pm
March 31, 2010
11:41 pm
April 16, 2010
10:20 am
April 28, 2010
1:08 pm
Whaddya think?