I needed a lightweight PHP library to pull images and image data from a public Google Picasa gallery. The result is a PHP class, based on this work, which was then used as a foundation for a PmWiki cookbook pmGallery. PmGallery allows you to pull Picasa based images from public albums into PmWiki, using a simple markup.

If you want full access to Picasa’s API you have a choice of client-side libraries, including one for PHP, using the Zend framework. The library itself is pretty big weighing in at 1.5Mb.


Download

Download v0.2.0
The API comes in two files – this is for future features. The library uses the PHP function simplexml_load_string, so it will only work under PHP5. Save both files in the same directory.

You’ll also need to create a directory “cache/” in the same location that you store feeder.php. Set permissions on the “cache/” directory to 755.

Using the Library

You’ll need to first include picasa.php5, then instantiate a new picasaAPI() object, and finally set various options:


require_once('picasa.php5');
$myPicasaParser = new picasaAPI();
$myPicasaParser->updateOption('tag', 'cars,trucks');
$myPicasaParser->updateOption('user', 'USER_NAME');

Other options you can set are:

Next, get a feed link (based on the options used above), and parse the feed:


$feedUrl = $myPicasaParser->createFeedUrl('ALBUM', false);
$picasa = $myPicasaParser->parseFeed( $feedUrl );

And finally, access elements corresponding to four namespaces:


for ($i=0; $i < count($picasa\['main'\]); $i++){

$image = $picasa['main'][$i];
$entry = $picasa['entry'][$i];
$gphoto = $picasa['gphoto'][$i];
$exif = $picasa['exif'][$i];

// DO OTHER PROCESSING...

}

Take a look in picasa.php5 to see what elements are available within each namespace.

Explanation

Picasa provides atom feeds of public albums and images. There are two basic feeds, one for albums, and one for images:

Caching the Feed

feeder.php is responsible for actually getting the feed from Picasa. Each unique feed is cached for a default of 7,200 seconds (2 hours). That prevents hitting Picasa too often, for feeds which probably don’t change that often. Old entries in the cache are cleaned out as they need to be replaced by newer feeds.

Parsing XML

Once we have the feed stored, we can use PHP’s built-in XML parser simplexml_load_string:


$feed = simplexml_load_string($feedXml);
$namespace = $feed->getDocNamespaces();

We should then be able to iterate through each element of the $feed array. Things aren’t quite that easy though, as Atom feeds use XML namespaces. These are documented over on the Picasa API Reference – have fun with that.

The entry and exif namespaces are two of the trickier elements to access. From picasa.php5:


$con_attr = $ns_media->group->content->attributes();
$feed_arr\['entry'\]\[$i\]\['url'\] = (string)$con_attr\['url'\];
$feed_arr\['entry'\]\[$i\]\['width'\] = (string)$con_attr\['width'\];

The key here is that the Picasa feed returns an HTML tag. The “attributes()” call parses each element of the tag, so we have access to the URL, width, height.

And on the exif namespace:


$exif=$item->children($namespace\['exif'\])->tags;
$feed_arr\['exif'\]\[$i\]\['distance'\] = (string)$exif->distance;

Here the exif data is stored within an elements called “tags”. Go figure.

So in conclusion…

If you made it all the way down here, then congratulations. If you’re stuck or you want to change the library to include additional elements, then the first thing to do is to take a look at the feed – either the cached version, or simply enter the feed URL into the browser. That’ll help you find the elements you need.

If you’re trying to find EXIF data, and your feed relies on tags, then welcome to Google bug-land – you can’t. Feel free to vote for this bug.

And lastly, as a final suicidal stab, Picasa make it impossible to generate RSS feeds, as their feeds are ordered in reverse chronological order – and there is no option to change it. Here’s the bug-fix request – vote away.