I was writing a bit of PHP code that was supposed to go fetch a Flickr feed and parse through it - making a slideshow of sorts with the images that got returned. Simple enough.
Call me crazy, but I’m a fan of using cURL over file_get_contents() to pull in remote data for APIs or whatever. So, I cURL a url that looks a lot like this:
Makes sense, right? Data comes back and I run a json_decode() against my return. That should create a pretty object for me to parse and display my pictures. Right? Unfortunately, no. The response from Flickr looks like this:
jsonFlickrFeed( ...valid JSON data here... )
Umm? No thank you. I really have no problem with extemporaneous data, as long as it’s valid. This isn’t. Finally, after about 25 minutes of researching web services that return JSON in strange and pesky ways, I discovered that Twitter and Flickr and some other random APIs will return this as some sort of a callback that has something to do with the days of yore before JSON was cool and eval() or veal() or foie_gras() were necessary to pick it apart. The point is that no JSON validator will pass it. Also, when you check the Flickr API Documentation for the method I’m using there’s no mention of how to get rid of it. They just say “Yes, sir. We speak JSON! It’s the JSON of the American Revolution!” Check it out for yourself here: http://www.flickr.com/services/feeds/docs/photos_public/ and here: http://www.flickr.com/services/feeds/ - no mention of weird options to return valid JSON instead, am I right?
However, the trick is that there is one! There’s an option “nojsoncallback.” Thanks to this well-buried answer on Stack Overflow (http://stackoverflow.com/quest…) I was able to discover that. Now if you do a search for “nojsoncallback” on Stack Overflow, you’ll wind up with a few similar answers where the helpful guy actually got credit, but until you know that nojsoncallback exists, best of luck to you.
To round this out, I’ll share my PHP code with you here: http://gist.github.com/2714140
Thanks for playing along today.