Gold Plated Blog Posts
Whatev.
Monday, November 12, 2007

Lightweight PHP Picasa API

As I've mentioned before, the Pictures section of this site is run through Picasa, Google's picture service. So I upload all my pictures to Picasa through a local application that they provide, I group them into albums, name them, give them a location, etc, and then they appear on CameronHinkle.com. This works out really well because their interface is nice for uploading photos and I don't have to pay for storage (I'm only using 13% of my total space and I can buy more if I need it). I also like that it's hosted in a central place with a lot of other photos because that means it's not sectioned off in its own little corner of the world. I don't usually get hung up on privacy concerns so the fact that I don't have control over the physical location of the photos does not bother me in the least.

Anyway, all of this magic is brought to you by Picasa's API. When I started this project, I was under the impression that Picasa, like Blogger, had a really slick API written in PHP that I could easily integrate with my site. It was going to be a piece of cake. Well, that turned out to not be the case. There was no existing PHP API, only one in XML, which meant I had to start the task of parsing lots of XML (which fortunately is really easy in PHP). To make things easier, I made my own PHP API that can really be used by anyone. So if you have a Picasa account and you know PHP, this makes it relatively simple to display your Picasa pictures on your website. And now for the first time, I'm publishing it on the web.

Click here to download my lightweight Picasa API in PHP.

The API is not great, it does exactly what I need it to do and not a whole lot more. However, it's extraordinarily easy to add on to. Basically it is set up with 4 types of objects that are simple to understand: Accounts, Albums, Images, and ImageCollections. Accounts have Albums, Albums have Images, and ImageCollections also have Images. Everything is pretty self explanatory except possibly ImageCollection, which is just a way to contain several images that are not from the same Album because there are different rules (like ImageCollections aren't given a title). The easiest way to understand how each class works is to look at it's constructor because that will tell you what to pass in to make a given object. Be aware that if you only need one photo, creating an entire Account object will take forever, so there's an easy way to create just a single Image object.

If you want to publish your uploaded photos on the web, this is perfect! If you want to upload your own photos, it's not so good, but it may be a good start. Hopefully someone needs to do more than I did and runs with the idea. However, I assume that there will be a Zend package available for Picasa within the next 6 or 12 months, at which point my API will be completely obsolete. Until then, enjoy!

Update 04/09/2008: This post is for Version 1.0 of my Lightweight PHP Picasa API. Version 3.0 has since been released, which includes a great deal of expanded functionality. Go get it here for more Picasa PHP fun!

UPDATE 12/2/2007: I eventually took the time to post some help with using this download and that article is posted here. I will probably offer additional help later.

Comments

Anonymous said...

Hi Cameron,
I have written a small script (using yours files) to create a list of all my picasa albums with their icon but I'm not able to give a link to each album. The name of the album is different from the href that I must give to the icon.
Have you got any suggestion?
thanks!

Andrea

Posted Saturday, November 8, 2008 at 7:42 AM.
Carbonracer said...

Hey Cameron,

just wanna say thanks for such a great work. I just got it running in a test environment. And it works just perfect. I already tried with the Zend API. But it was just to complex for my target. So I am glad you made that job. Easy to use and self explaining source code. Great!!! And besides you created a great info pool with this blog. Loved reading it. Hope, you'll have time (and be motivated) to continue working on it. Eventhough I couldn't point to things having to be improved.

Greez, Andi

Posted Thursday, July 3, 2008 at 2:59 AM.
Anonymous said...

I am not so much good in this things but i like to learn with other examples ..i checked this 4 files but i dont know how to include api & album id to this??sorry for my simple doubt ..thanks you

Posted Monday, December 31, 2007 at 7:30 AM.
curiousMe said...

hi Cameron,

i have downloaded your 4 files for picasaweb API in PHP. i somehow couldn't get it to work. I have written a simple script to instantiate the class but i get a lot of errors.

here is what my script that calls the classes :

require_once 'Account.php';
$myPicasa = new Picasa_Account("http://picasaweb.google.com/xxxxxxxx");

the error i got when i tried is quite a lot:


Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: Entity: line 7: parser error : EntityRef: expecting ';' in /Picasa/Account.php on line 64

Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: aweb.google.com/data/feed/base/user/xxxxxxxxxx kind=album&alt in /home/hensono/public_html/assets/Picasa/Account.php on line 64

Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: ^ in /Picasa/Account.php on line 64

would really appreciate your help.

Posted Wednesday, December 19, 2007 at 4:51 PM.
Cameron said...

Sure, I can give an example. I should probably take some time and dedicate a whole post on how to do it, but for now I'll just do it here.

The API uses the query structure outlined in Google's documentation. Each data type in my API takes a query (or raw XML) as an argument for the constructor. You form a picasaweb.google.com query URL, send it to the constructor of the data type you're going to use, and it will return the data associated with the query you sent. For instance, here is the code I use to get an array of public albums in my Picasa account:

public function getAlbumsForDefaultAccount() {
return new Picasa_Account(Cam_Util_PictureUtil::$BASE_QUERY_URL.'?kind=album');
}

You can see I've stored the base part of the query (which doesn't ever change throughout my site) into a static field. Then to put the albums into an array:

$account = $picServ->getAlbumsForDefaultAccount();

$account is then an array that I can use in a foreach just like any other array. I use the getter methods for each element in the array to access the individual fields in the Album class.

One thing to keep in mind is that when you create an Account object, it creates each Album object but does not create Picture objects within each Album. This is true in the atom feed and I had initially changed it so that it created each picture object for the entire account, but the performance was terrible.

Hope this helps!

Posted Tuesday, November 27, 2007 at 1:06 PM.
Anonymous said...

hello, can show us example on how to use your php files? thank you.

Posted Tuesday, November 27, 2007 at 7:23 AM.
The articles in this blog are authored by Cameron Hinkle, Software Engineer for Nike. The thoughts and opinions expressed are not shared by Nike or any of its affiliates.
Favorite links: