Knowpapa.com - a developer's blog

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















DSC_0280

mayahara_family posted a photo:

DSC_0280

Glen Ellyn Tournament 2017


DSC_0020

mayahara_family posted a photo:

DSC_0020

Glen Ellyn Tournament 2017


DSC_0190

mayahara_family posted a photo:

DSC_0190

Glen Ellyn Tournament 2017


DSC_0134

mayahara_family posted a photo:

DSC_0134

Glen Ellyn Tournament 2017


DSC_0377

mayahara_family posted a photo:

DSC_0377

Glen Ellyn Tournament 2017


DSC_0218

mayahara_family posted a photo:

DSC_0218

Glen Ellyn Tournament 2017


DSC_0436

mayahara_family posted a photo:

DSC_0436

Glen Ellyn Tournament 2017


DSC_0389

mayahara_family posted a photo:

DSC_0389

Glen Ellyn Tournament 2017


DSC_0005

mayahara_family posted a photo:

DSC_0005

Glen Ellyn Tournament 2017


DSC_0350

mayahara_family posted a photo:

DSC_0350

Glen Ellyn Tournament 2017


DSC_0065

mayahara_family posted a photo:

DSC_0065

Glen Ellyn Tournament 2017


DSC_0300

mayahara_family posted a photo:

DSC_0300

Glen Ellyn Tournament 2017


DSC_0385

mayahara_family posted a photo:

DSC_0385

Glen Ellyn Tournament 2017


DSC_0057

mayahara_family posted a photo:

DSC_0057

Glen Ellyn Tournament 2017


DSC_0077

mayahara_family posted a photo:

DSC_0077

Glen Ellyn Tournament 2017


DSC_0093

mayahara_family posted a photo:

DSC_0093

Glen Ellyn Tournament 2017


DSC_0203

mayahara_family posted a photo:

DSC_0203

Glen Ellyn Tournament 2017


DSC_0073

mayahara_family posted a photo:

DSC_0073

Glen Ellyn Tournament 2017


DSC_0014

mayahara_family posted a photo:

DSC_0014

Glen Ellyn Tournament 2017


DSC_0286

mayahara_family posted a photo:

DSC_0286

Glen Ellyn Tournament 2017