The Glass is Too Big - Home

5 Lessons in Getting Started with the Flickr API and PHP

Originally published on: 12/16/2005 9:16:08 PM



The Flickr API has some seriously userful capabilities. My prototype photo finder just scratches the surface of the API itself, much less what you could do when blending it with other API's. For instance, if you used the Yahoo keyword extractor to analyze your post to get the list of keywords to use in mining Flickr, you could quickly get photos just by submitting your article draft. That could go further by using Technorati's API or delicious to get the other keywords, comparing all of the sets of keywords to get overlap. Enough rambling, on to the geeking.

In the process of building my prototype of the photo finder, I learned a few lessons that could save you some energy, frustration and time.



  1. You need a key, but the key isn't necessarily all you need.

  2. Authentication is a pain.

  3. Most of the PHP implementations are a pain.

  4. When an implementation doesn't suck, it's a glorious thing.

  5. Crank the caching up.





First of all, you do need an API key to do anything with it. It's a pretty basic form that will give you a long md5-ish string. Write it down, but you can get it later. Now, if you start digging through the documentation, you'll start seeing references to the "secret" value you need to combine with the key for some stuff.

What appears to be missing is exactly where you get the darn thing. Now, I've been accused more than once of "not looking hard enough", but that usually refers to being able to notice the fact that the dishes in the dishwasher were clean. However, when it comes to finding stuff online, I usually don't have that problem. I did manage to find it, but it took me WAY longer than it should have.

What it comes down to is that you need to go to "your" API keys and do the authentication thing and you'll then get the secret key.

OK, so you've finally got an API key, a secret key and are ready to start authenticating. You still have to send those 2 things along with the user over to Flickr where they'll OK your use of their account and then direct back to the URL you gave to get your secret key and giving you back a token to use with subsequent requests. Intellectually, I understand why this is the way they set it up (it's more secure and easy for the user), but it's a serious pain to wade through the first time.

The good news is that you don't need the authentication layer to do lots of useful stuff. So, don't bother with the above until you actually need user account access.

So, you've got your keys, you've messed around with raw XML requests and you're ready to hook it up to PHP. Well, on the Flickr API pages, there are 3 implementations of the API in PHP. I started with the wrong 2. I figured either the PHP5 one (more up to date and Object Oriented) or the PEAR one (sort of official) would be best. Actual reality? Not so much. The PEAR one first didn't want to install and then, the little commandline script that I had to log onto the server to run (after I'd logged off from installing) needed to do the authentication thing. Except it threw fatal errors all over the place. So, I grabbed the other one.

That one worked, but brings back this "SimpleXML" object that is a nightmare to dig through. What you need to build image URL's is 3-4 variables. Yet, the object that comes back is gigantic and nested in one of the strangest ways I've ever seen. You get basically, these alternating objects and arrays as you go down the levels. Each level's parent is a different type than it is and so are it's children. So, the top is an object, its children arrays, their children objects, their grandchildren arrays, etc. This means that if you just want to drill down, you actually have to alternate your syntax ($object->child for objects and $array['child'] for array entries) or do conversions. Given how simple the actual XML is that comes back, this is just plain overkill.



So, on to the 3rd option and the next lesson. phpFlickr just works and it's amazing how refreshing that can be after the previous 2 experiences. It was glorious. I dropped it in place, configured a cache directory and started fetching stuff from Flickr. A few print_r()'s and you can see just how much simpler the results are.

Once I had the right library, it was pretty easy to whip through what I needed to build the prototype. No big deal really. However, the one lesson I will note is just how big a difference caching makes on the speed of requests. I turned mine way up to 24 hours and it's really zipping along (for the query portion anyway).

If you learn from my pain, you can get up and running with PHP and Flickr without feeling like screaming.

Comments

Hendrik
commented on 12/21/2005
Hehe, cool photo :)
Greg
commented on 12/27/2005
I just wrote that the flickr API is worthless (http://www.blendedtechnologies.com/flickr-api-worthless/54)

You're article has me rethinking that now. If I do this secret key stuff, then can I get the url of images?

-Greg

J Wynia
commented on 12/28/2005
Yeah, you need to look at the API documentation again. flickr.photos.search gives you the info you need to build URL's. How to assemble them is documented here.
Greg
commented on 12/28/2005
Thanks! That worked!

www.pinerophotography.com is the site I'm doing all this flickr API stuff for, feel free to check it out in a few weeks (assuming I keep up my pace).

Answer My Searches » Blog Archive » flickr API - How to get URL of a photo
commented on 12/28/2005
[...] Thanks to J Wynia for the help.   Trackback  Permalink   Filed under: Python, flickr Leave a Comment [...]
Answer My Searches » Blog Archive » flickr API - How to get URL of a photo
commented on 1/2/2006
[...] to J Wynia for the help.   Trackback  Permalink   Filed under: Python, flickr Leave aComment [...]
webserv » links
commented on 3/27/2006
[...] http://www.wynia.org/wordpress/2005/12/16/5-lessons-in-getting-started-with-the-flickr-api-and-php/ [...]
marinaccio
commented on 7/27/2008
Thanks for the article!

-marinaccio

blog comments powered by Disqus
© 2003- 2010 J Wynia. Very Few Rights Reserved.