FriendFeed Badge PHP Script
This is a PHP script that obtains your most recent lifestream activity from FriendFeed, and provides an easy way to show it on your own web site.
This script depends on the Cache class. Grab that first.
<?php
$friendfeed = new FriendFeed(new Cache("/tmp/friendfeed"), "manastungare");
$entries = $friendfeed->getEntries(10);
foreach ($entries as $entry) {
echo($entry);
}
?>
<?php
class FriendFeed {
var $user_, $feed_;
private static $verbs_ = array(
'googlereader' => 'Shared <a href="%s">“%s”</a> on Google Reader.',
'twitter' => '<a href="%s">Tweeted</a> “%s”.',
'amazon' => 'Added <a href="%s">“%s”</a> to my wishlist on Amazon.',
'youtube' => 'Added <a href="%s">“%s”</a> to favorites on YouTube.',
'flickr' => 'Added <a href="%s">“%s”</a> to favorites on Flickr.',
'delicious' => 'Bookmarked <a href="%s">“%s”</a> on Del.icio.us.',
'facebook' => 'Wrote a note on Facebook: <a href="%s">“%s”</a>.',
'blog' => '<strong>Posted a blog entry, <a href="%s">“%s”</a></strong>.'
);
public function __construct($cache, $user, $howMany = 100) {
$this->cache_ = $cache;
$this->user_ = $user;
$friendfeedJson = $this->cache_->get(
sprintf("http://friendfeed.com/api/feed/user/%s?format=json&start=0&num=%d",
urlencode($this->user_),
$howMany));
$this->feed_ = json_decode($friendfeedJson);
}
public function getEntries($howMany = 10) {
$entries = array();
$count = 0;
foreach ($this->feed_->entries as $id => $entry) {
// I want to ignore Twitter. Personal decision.
if ("twitter" === $entry->service->id) continue;
if (++$count > $howMany) break;
$verb = isset(FriendFeed::$verbs_[$entry->service->id])
? FriendFeed::$verbs_[$entry->service->id]
: 'Shared <a href="%s">“%s”</a> on ' . $entry->service->name . '.';
$linkifiedTitle = preg_replace("/http:\/\/(.*?)\/[^ ]*/",
'<a href="\\0">\\0</a>', $entry->title);
$entries[] = sprintf($verb, $entry->link, $linkifiedTitle);
}
return $entries;
}
}
?>
