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















Old Growth Olympic Forest

brianstowell posted a photo:

Old Growth Olympic Forest

A winding path between old growth trees and ferns in Olympic National Park, Washington

Available as a print on:
Society6
Redbubble

More adventures:
www.brianstowell.com
Instagram: @brianstowell
Tumblr: @brianstowell
Facebook: /brianstowellphoto
500px: /brianstowell


2017 SUPER Plunge - ST-1330

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1330

Photo by Sam Tang


2017 SUPER Plunge - ST-1073

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1073

Photo by Sam Tang


2017 SUPER Plunge - ST-1109

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1109

Photo by Sam Tang


2017 SUPER Plunge - ST-1184

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1184

Photo by Sam Tang


2017 SUPER Plunge - ST-2907

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-2907

Photo by Sam Tang


2017 SUPER Plunge - ST-0987

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-0987

Photo by Sam Tang


2017 SUPER Plunge - ST-0971

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-0971

Photo by Sam Tang


2017 SUPER Plunge - ST-1051

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1051

Photo by Sam Tang


2017 SUPER Plunge - ST-1099

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1099

Photo by Sam Tang


2017 SUPER Plunge - ST-1044

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1044

Photo by Sam Tang


2017 SUPER Plunge - ST-1064

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1064

Photo by Sam Tang


2017 SUPER Plunge - ST-1111

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1111

Photo by Sam Tang


2017 SUPER Plunge - ST-1323

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1323

Photo by Sam Tang


2017 SUPER Plunge - ST-1331

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1331

Photo by Sam Tang


2017 SUPER Plunge - ST-1338

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1338

Photo by Sam Tang


2017 SUPER Plunge - ST-1049

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1049

Photo by Sam Tang


2017 SUPER Plunge - ST-1327

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1327

Photo by Sam Tang


2017 SUPER Plunge - ST-1412

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1412

Photo by Sam Tang


2017 SUPER Plunge - ST-1336

Special Olympics ILL posted a photo:

2017 SUPER Plunge - ST-1336

Photo by Sam Tang