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















Port Angeles C2158

All h2o posted a photo:

Port Angeles C2158

Pacific Northwest Exposed
Port Angeles, WA with the Olympic Mountains in the background.


FED 5V & Industar 61L/D

PlanktonMSC posted a photo:

FED 5V & Industar 61L/D

1979 FED 5V [1975 - 1990]
Dedicated to 80' Olympics in Moscow.
Lens - 2.8/53 Industar 61L/D
Made in USSR by FED.


Casey Wasserman

Beauboi3 posted a photo:

Casey Wasserman


Forest C2345

All h2o posted a photo:

Forest C2345

Pacific Northwest Exposed
A walk through the Olympics.


Lake Placid New York ~ Dramatic Refection on the Water

Onasill ~ Bill Badzo posted a photo:

Lake Placid New York  ~  Dramatic Refection on the  Water

The Ausable River also known as AuSable River and originally written as "Au Sable", runs in the U.S. state of New York, from the Adirondack Mountains and past the village of Lake Placid and Au Sable Forks to empty into Lake Champlain It has an East and West branch that join at Au Sable Forks. The river forms a partial boundary between Clinton County and Essex County. The Ausable River is known for its gorge, Ausable Chasm, located a few miles east of Keeseville. 1095


29673

benbobjr posted a photo:

29673

The 2012 Olympic Torch Relay, Newport, Lincoln, Lincolnshire.


Minister for the Olympics

alexliivet posted a photo:

Minister for the Olympics


LAC_5746

@IBSFsliding posted a photo:

LAC_5746


LAC_7074

@IBSFsliding posted a photo:

LAC_7074


LAC_5415

@IBSFsliding posted a photo:

LAC_5415


LAC_6560

@IBSFsliding posted a photo:

LAC_6560


LAC_6514

@IBSFsliding posted a photo:

LAC_6514


LAC_5848

@IBSFsliding posted a photo:

LAC_5848


LAC_5941

@IBSFsliding posted a photo:

LAC_5941


LAC_6656

@IBSFsliding posted a photo:

LAC_6656


LAC_7122

@IBSFsliding posted a photo:

LAC_7122


LAC_5729

@IBSFsliding posted a photo:

LAC_5729


LAC_5603

@IBSFsliding posted a photo:

LAC_5603


LAC_6390

@IBSFsliding posted a photo:

LAC_6390


LAC_7098

@IBSFsliding posted a photo:

LAC_7098