Parsing Flickr Feeds using PHP Simple XML API

With over 5 billion photos (many with valuable metadata such as tags, geolocation, and Exif data) and a large section of it with creative common license – the flickr feed can be a handy resource for developers to enhance usability of their web applications.

The first thing to do is to grab the flickr feed for images with creative common license. The feed for Flick is:

http://api.flickr.com/services/feeds/photos_public.gne

The flickr feed supports the following query parameters:

id (Optional)
A single user ID. This specifies a user to fetch for.
ids (Optional)
A comma delimited list of user IDs. This specifies a list of users to fetch for.
tags (Optional)
A comma delimited list of tags to filter the feed by.
tagmode (Optional)
Control whether items must have ALL the tags (tagmode=all), or ANY (tagmode=any) of the tags. Default is ALL.
format (Optional)
The format of the feed. See the feeds page for feed format information. Default is Atom 1.0.
lang (Optional)
The display language for the feed. See the feeds page for feed language information. Default is US English (en-us).

Now assuming that a page in our web-application relates to latest news from the London Olympics. So we can look for the latest photographs in the feed using the tag olympics. Our new feed URL then becomes:

http://api.flickr.com/services/feeds/photos_public.gne?tags=olympics

Now all we need is to parse the feed content. PHP5 comes with a powerful bundled extension API called simpleXML which allows for parsing contents the pure OOP way.

echo "Latest Images from London Olympics";
$feed = file_get_contents 
("http://api.flickr.com/services/feeds/photos_public.gne?tags=olympics");
$xmlparse = simplexml_load_string ($feed);
// now time to display the images
foreach ($xmlparse as $item)
{
echo "link['href']}'>".$item->title."
";
echo $item->content."
"; // this line takes care of the attribution requirement for the photos.
}

That is it – and the output would look something like this (you can use css to stylise it ):

Latest Images from London Olympics















Tower Bridge

caro.gill posted a photo:

Tower Bridge

London Olympics 2012


Stagecoach Merseyside & South Lancashire 18002 SF53BZJ

Will Swain posted a photo:

Stagecoach Merseyside & South Lancashire 18002 SF53BZJ

Seen outside Chester Depot having recently been transferred from Scotland
10th December 2016

Former Stagecoach Western, new at Ardrossan to work service A1

18002 worked in Newcastle covering for E400s down in London for the 2012 Olympic Games

Also seen with Stagecoach Western in Dumfries in June 2011 - www.flickr.com/photos/68628359@N04/6673599065/in/photolis...


Quinault_River_-_Lake_Quinault

davidrockey1 posted a photo:

Quinault_River_-_Lake_Quinault


IMG_6409-1

CAL ROTARACT posted a photo:

IMG_6409-1


IMG_6537-1

CAL ROTARACT posted a photo:

IMG_6537-1


IMG_6433-1

CAL ROTARACT posted a photo:

IMG_6433-1


IMG_6547-1

CAL ROTARACT posted a photo:

IMG_6547-1


IMG_6403-1

CAL ROTARACT posted a photo:

IMG_6403-1


IMG_6441-1

CAL ROTARACT posted a photo:

IMG_6441-1


IMG_6395-1

CAL ROTARACT posted a photo:

IMG_6395-1


IMG_6444-1

CAL ROTARACT posted a photo:

IMG_6444-1


IMG_6423-1

CAL ROTARACT posted a photo:

IMG_6423-1


IMG_6489-1

CAL ROTARACT posted a photo:

IMG_6489-1


IMG_6390-1

CAL ROTARACT posted a photo:

IMG_6390-1


IMG_6429-1

CAL ROTARACT posted a photo:

IMG_6429-1


IMG_6381-1

CAL ROTARACT posted a photo:

IMG_6381-1


IMG_6516-1

CAL ROTARACT posted a photo:

IMG_6516-1


IMG_6417-1

CAL ROTARACT posted a photo:

IMG_6417-1


IMG_6375-1

CAL ROTARACT posted a photo:

IMG_6375-1


IMG_6380-1

CAL ROTARACT posted a photo:

IMG_6380-1