Member Sign In
Not a member?

A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.


It's fast and free.

Webmonkey is a property of Wired Digital.
processing...
Join Webmonkey

Please send me occasional e-mail updates about new features and special offers from Wired/Webmonkey.
Yes No

Please send occasional e-mail offers from Wired/Webmonkey affiliated web sites and publications, and carefully selected companies.
Yes No

I understand and agree that registration on or use of this site constitutes agreement to Webmonkey's User Agreement and Privacy Policy.
Webmonkey is a property of Wired Digital.
processing...

Retrieve Sign In

Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.

or
Webmonkey is a property of Wired Digital.
processing...

Welcome to Webmonkey

A private profile page has been created for you.
As a member of Webmonkey, you can now:
  • edit articles
  • add to the code library
  • design and write a tutorial
  • comment on any Webmonkey article
Close
Webmonkey is a property of Wired Digital.

Sign In Information Sent

An e-mail has been sent to the e-mail address registered in this account.
If you cannot find it in your in-box, please check your bulk or junk folders.
Sign In
Webmonkey is a property of Wired Digital.

Make an Uber-Blog Using the FriendFeed API

/skill level/
/viewed/
0 Times

If you've ever wanted to collect all your various kinds of info spread across the web -- Twitter posts, Flickr photos, YouTube videos, Delicious link, etc -- you know that while there are some very nice APIs for all those services, but interacting with all of them takes time.

Even if you write a generic connection client that can handle all of your different APIs and formats, parsing the data is going to require a separate chunk of code for each. For example some APIs return publication dates as pub_date, some pubDate, some date_post and so on.

Surely there must be a better way to collect and combine your data into your own "tumblelog," as the kids call it?

Well there is. The key is data normalization. Rather than write the code up yourself, why not take advantage of a service that already does it for you -- FriendFeed.

Contents

About FriendFeed

Essentially, FriendFeed is a tumblelog. You feed in RSS URLs, it grabs the content and displays it on your FriendFeed page. How about those people who don't use FriendFeed? What if you want to all of your public updates from all the various blog services and show it in one public place?

That's where the FriendFeed API shines -- it eliminates the need to filter your data through different APIs. Instead, it processes your data in a single, easy to parse format.

What we're going to do is connect to the FriendFeed API and pull in our data so we get videos, photos, twitter messages, links and more in the single API call.

Getting Started

We'll use PHP to interact with FriendFeed since it's probably the most common programming language available through cheap shared hosting plans. However, everything we're going to do could also be done using your favorite scripting language instead. There are even very nice API libraries available for PHP, Python and C#.

Head over to the FriendFeed API page on Google Code and grab the PHP library. Now create a new folder on your remote server and drop the FriendFeed PHP folder in it.

The last thing we need to do is grab our Remote Key. A FriendFeed Remote Key is just like a password, except that it is only used for third party applications like our script. Head to this page to grab yours.

Let's create a new file and write some code. I labelled mine ff_client.php, but how you name it is up to you. Now paste in this code:

require_once("php/friendfeed.php");
require_once("php/JSON.php");
 
//Connect
$friendfeed = new FriendFeed("yourusername", "yourpassword");
$feed = $friendfeed->fetch_user_feed("yourusername", null, 0, 30);
foreach ($feed->entries as $entry) {
  //grab the name of the source and the icon image
  $data .= '<img src="' . $entry->service->iconUrl . '" alt="' . $entry->service->name . '" />';
 
  //grab the entry title and link back to the friendfeed post
  $data .= $entry->service->name.'<a href="'.$entry->link.'">'.$entry->title.'</a>';
 
  //grab the date
  $data .= date('m d Y', $entry->published);
  
}
print $data

So what did we just do? Well this is a very basic example of how to grab and display some FriendFeed data. First we include the PHP libraries we just downloaded. Then we connect to FriendFeed using the provided fetch_user_feed method to grab our data.

The parameters we set in the code are the username, a specific service to look for (which is null so we get all the services), a date to start and the number of entries to grab. In our case, we are grabbing the 30 most recent entries.

Once we have the data, we need to loop through and parse the individual posts. Here's how we do it: We grab the name and icon of the originating service, the title of the entry, a link back to the original page and finally the date it was posted.

Now all we need to do is print our $data variable and we'll have something very similar to our FriendFeed homepage.

Go ahead and load the page in your browser and make sure everything is working.

[Note: for simplicity of example, we've stored our username and password in our PHP file. In practice, it's a very bad idea. Its much better to store the username and password data in file which lives outside your public web root and the import it into the file.]

Getting Fancier

While the title and a link back to the page is nice, wouldn't it be cooler if we just displayed the content -- the photo and link or whatever?

All it takes is a little code to our loop. In between the line where we grab the entry and the line where we grab the date, add this code:

foreach ($data->media as $media) {
	foreach ($media->thumbnails as $thumb) {
        $data .= '<img src="'.$thumb->url.'" />'
    }
}

This chunk of code simply digs deeper into our JSON return data call to grab any thumbnails provided for our posts.

If you want to grab the actual posted item, you could do essentially the same thing, but the node you want to target is $data->content. Also bear in mind that you'll have to use some if statements to plugin the appropriate tags for embedding different types of media.

Ideas for going further

What we've done is admittedly pretty simple, but should give you a general overview of how to interact with the FriendFeed API. However, rather than simply grabbing and displaying our data, a more robust solution would be to store it in a database.

This way when a visitor hits our site, we don't have to turn around and hit FriendFeed (FriendFeed doesn't take these kind of repeated queries too kindly). As an added bonus, throwing all the data in our own database gives us a handy backup should FriendFeed ever shutter its API doors.

Also be sure to check out the FriendFeed API documentation since there are quite a few more methods worth investigating. Want to get really fancy? You could even pull in all the comments people have posted on your FriendFeed entries and display them inline on your own site (provided they're public comments).

Of course there are also API methods to push content to FriendFeed, so if you want to push your blog entries to FriendFeed, you can. However, just adding the RSS feed through FriendFeed's on-site interface is probably much easier.

Conclusion

Hopefully you have some idea of how you can leverage FriendFeed as a data normalization service and avoid dealing with dozens of different APIs just to grab your web content. Of course this method isn't without its tradeoffs -- for instance FriendFeed doesn't carry through all the fancy EXIF data you can access through the Flickr API -- but for the basic tumblelog it will likely suffice.

If you've got ideas for other ways to use the FriendFeed API be sure to jump in and add your suggestions.

  • This page was last modified 00:32, 19 August 2008.
Edit this article
Reddit Digg
 

/related_articles/

See more related articles

Subscribe now

Special Offer For Webmonkey Users

WIRED magazine:
The first word on how technology is changing our world.

Subscribe for just $10 a year