Google Buzz Badge PHP Script

This is a PHP script that obtains your most recent lifestream activity from Google Buzz, and provides an easy way to show it on your own web site.

This script depends on the Cache class. Grab that first.

<?php
  $buzz = new Buzz(new Cache("/tmp/buzz"), "manastungare");
  $items = $buzz->getItems(10);
  foreach ($items as $item) {
    echo($item);
  }
?>
<?php
  class Buzz {
    var $user_, $feed_;

    private static $verbs_ = array(
      'twitter' => 'Tweeted &#8220;%s&#8221;',
      'google reader' => 'Shared &#8220;%s&#8221; on Google Reader.',
      'youtube' => 'Added &#8220;%s&#8221; to favorites on YouTube.',
      'flickr' => 'Added &#8220;%s&#8221; to favorites on Flickr.',
      'blog' => '<strong>Posted a blog entry, &#8220;%s&#8221;</strong>.'
    );

    public function __construct($cache, $user) {
      $this->cache_ = $cache;
      $this->user_ = $user;
      $url = sprintf('https://www.googleapis.com/buzz/v1/activities/%s/@public?alt=json',
          urlencode($this->user_));
      $buzzJson  = $this->cache_->get($url);
      $this->feed_ = json_decode($buzzJson);
    }

    public function getItems($howMany = 10) {
      $items = array();
      $count = 0;

      foreach ($this->feed_->data->items as $id => $item) {
        if (++$count > $howMany) break;
        $title = str_replace('<br />', '&mdash;', $item->object->content);
        $source = strtolower($item->source->title);
        $verb = isset(Buzz::$verbs_[$source])
              ? Buzz::$verbs_[$source]
              : 'Shared %s on ' . $source;

        $items[] = sprintf($verb, $title);
      }
      return $items;
    }
  }
  ?>