Flickr -> PHP -> XML

This post will serve as support to another post I'll be writing in the next few days. Today we will be looking at the Flickr API, making some API calls, and processing the data to make it usable. Before you start working with the API, I strongly suggest you grab a non-commercial API key from Flickr. This key will be required to make most calls to the API as an attempt to limit the rate an application can tax Flickr's servers.

Now that we've got our API key ready to go, we can start making API calls, exciting! So what exactly can we pull from the Flickr API? If you look at this page, all the API Methods are listed on the right side. Since this API is pretty robust, we can essentially recreate Flickr using this API, but that is strictly against their Developer Terms of Service so we'll just play with a basic API call for now.

Perhaps the most interesting of the APIs we can access is the interestingness method. What this method does is return the list of interesting photos for the most recent day or a user-specified date. In other words, it returns a list of pictures that are pretty cool and change every day. This is a good set to be working with since it is changing every day. The URL request is as follows:


  • api_key (required) - the key we got from creating our non-commercial application
  • date (optional) - the date we want to grab in YYYY-MM-DD format, returns today's image if omitted
  • extras (optional) - list of extra information to get for each image, see this
  • per_page (optional) - number of photos to return per page, returns 100 if omitted, max 500
  • page (optional) - the page of results to return, returns 1 if omitted


Returns 100 interesting photos from January 1st, 2010. The request will return something like this:

<rsp stat="ok">
<photos page="1" pages="5" perpage="100" total="500">
	<photo id="4235035896" owner="21586233@N07" secret="a003fb3c72" server="4020" farm="5" title="so long 2009,  hello 2010!" ispublic="1" isfriend="0" isfamily="0" />
	<photo id="4234605704" owner="12634360@N08" secret="5d46debe80" server="2639" farm="3" title="Water splash" ispublic="1" isfriend="0" isfamily="0" />
	<photo id="4234917093" owner="78962602@N00" secret="b8a6051cb9" server="2654" farm="3" title="Allister Ann" ispublic="1" isfriend="0" isfamily="0" />

What's Next?

Well, looking at the returned values, there's no actual image link. There are a bunch of tags like id, secret, server, and farm that we will manipulate to build our link. The structure of Flickr URLs is:


Most required parameters are included in the XML, but the bit at the end, [mstzb], is for requesting the size of the image returned. The key is m-small, s-small square, thumbnail, z-medium, b-large. Feel free to experiment with the sizes. Here is an example of a URL I put together using the first XML record returned. Note the _b added before the .jpg to ask Flickr for the large image.

Take a breather! Cool, we're now able to understand the format of the returned by the Flickr API. Next we will look at making this more useful. As of PHP5, a nice library called simplexml has been provided to easily work with XML files. It's so easy to work with XML now by using this library that we can generate usable image links in just a few links of code. Here is an example that outputs a much more simple, and usable XML file.

    $xml = simplexml_load_file("{yourKey}&&format=rest");
    $output = "<images>";
    foreach($xml->children()->children() as $child) {
		$imgsrc = "http://farm" . $child['farm'] . "" . $child['server']. "/" . $child['id'] . "_" . $child['secret'] . "_b.jpg";
		$output .= "<image>";
		$output .= "<url>$imgsrc</url>";
		$output .= "<title>" . $child['title'] . "</title>";
		$output .= "</image>";
    $output .= "</images>";

Let us go step-by-step. First we load the XML of our previously-built request by using simplexml's simplexml_load_file() function. We start our $output variable with the opening tag that will contain all our images. Next we jump into a foreach loop that will iterate through every child element (image) that the API returned. By using children()->children(), we are able to get the children elements that are two levels deep in the XML tree; you can look at it as if it helps you understand. For each element we iterate through, we build an entry populated with the image source and the title. After we finish all our images we close off the tag and output our $output variable containing the XML. It will look something like this:

	<title>so long 2009, hello 2010!</title>

Wrapping Up

It's easy to see how much more use we can now get out of this XML layout. With the PHP I provided you with, you can do many other things such as storing the images in a database, displaying them all in a page, downloading them, really anything you could think of!

Stay tuned for my next post where I will begin assembling a slideshow in Actionscript/Flash that utilizes the process XML we just created!