Originally published on: 12/16/2005 9:16:08 PM
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.
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.
You're article has me rethinking that now. If I do this secret key stuff, then can I get the url of images?
-Greg
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).
-marinaccio