Fetching data from Digg, Flickr and YouTube using APIs

This week my aim was to be able to retrieve data from Digg, Flickr and YouTube for a given username. Whilst doing this, I have been discovering the powers of OOP, APIs and SimpleXML.

This is the first time I have used any of these technologies, so it took a bit of time and learning to figure out how to use them in order to fulfil my aims.

YouTube

YouTube was the first site I attempted to get data from which proved to be quite difficult as I didn't really have any idea how to do it. From the YouTube API docs I worked out how to query YouTube and that the data was returned as XML (Example).

I started looking at the XML Parser functions in the PHP documentation, to find out how to parse XML, but this proved to be complicated and difficult to understand. I later found out about the SimpleXML functions which make parsing XML a lot simpler. However, SimpleXML requires PHP 5 so I am having to develop the web site on my own server as the university server is still running PHP 4.

Demo

Flickr

As I had already done the hard work in figuring out how to get the basics to work whilst using the YouTube API, it was a lot easier to get the Flickr API working. It uses the same method of querying data (GET request) and returns the results as XML.

The only issue I had with the API is that first you need retrieve the user ID for a user before you can fetch their latest photos as you can't use the username to do this. Flickr allows users to change their username, so using the user ID instead is a good idea as it's never going to change, whereas the username could.

One of the good things about the Flickr API is that it allows you to see the amount of queries you are using over a period of time on the Flickr web site so you can keep track of usage easily.

Demo

Digg

Again, this worked very similarly to YouTube and Flickr so was quite easy to do. The only issue I had with the Digg API is that when querying a user's dugg stories it would only return the ID of the story, not the story title or link. This meant having to make a separate query for each story to find out these details.

Demo

Conclusion

I have learnt a lot from interfacing with these various sources which I hope to expand upon as I continue to build Trakr. I have chosen to stick with just three for now to keep things simple when integrating them into the rest of the web site. I will add more sources once I have integrated these successfully.

Each of these sources have been wrapped in a class with functions to retrieve data and to check that a user exists at the source.

Posted: 28th October 2007 18:20