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















Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Visit from Mascot's Ash & Summer at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography


Team 1 v Team 2

#Sheffield17 posted a photo:

Team 1 v Team 2

Crowd & volunteers enjoying the Football at the Special Olympics at Sheffield & Hallam University Sports Park, Sheffield, UK. 10/08/2017 . Photo: SB Sports Photography


Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Visit from Mascot's Ash & Summer at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography


Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Visit from Mascot's Ash & Summer at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography


Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Visit from Mascot's Ash & Summer at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography


Team 1 v Team 2

#Sheffield17 posted a photo:

Team 1 v Team 2

Volunteers from the Special Olympics Football at Sheffield & Hallam University Sports Park, Sheffield, UK. 11/08/2017 . Photo: SB Sports Photography


Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Visit from Mascot's Ash & Summer at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography


Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Visit from Mascot's Ash & Summer at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography


Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Visit from Mascot's Ash & Summer at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography


Team 1 v Team 2

#Sheffield17 posted a photo:

Team 1 v Team 2

Crowd & volunteers enjoying the Football at the Special Olympics at Sheffield & Hallam University Sports Park, Sheffield, UK. 10/08/2017 . Photo: SB Sports Photography


Team 1 v Team 2

#Sheffield17 posted a photo:

Team 1 v Team 2

Crowd & volunteers enjoying the Football at the Special Olympics at Sheffield & Hallam University Sports Park, Sheffield, UK. 10/08/2017 . Photo: SB Sports Photography


Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Spectators at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography


Team 1 v Team 2

#Sheffield17 posted a photo:

Team 1 v Team 2

Crowd & volunteers enjoying the Football at the Special Olympics at Sheffield & Hallam University Sports Park, Sheffield, UK. 10/08/2017 . Photo: SB Sports Photography


Team 1 v Team 2

#Sheffield17 posted a photo:

Team 1 v Team 2

Crowd & volunteers enjoying the Football at the Special Olympics at Sheffield & Hallam University Sports Park, Sheffield, UK. 10/08/2017 . Photo: SB Sports Photography


Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Visit from Mascot's Ash & Summer at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography


Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Visit from Mascot's Ash & Summer at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography


Team 1 v Team 2

#Sheffield17 posted a photo:

Team 1 v Team 2

Venue pictures from the Special Olympics Football at Sheffield & Hallam University Sports Park, Sheffield, UK. 11/08/2017 . Photo: SB Sports Photography


Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Visit from Mascot's Ash & Summer at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography


Team 1 v Team 2

#Sheffield17 posted a photo:

Team 1 v Team 2

Crowd & volunteers enjoying the Football at the Special Olympics at Sheffield & Hallam University Sports Park, Sheffield, UK. 10/08/2017 . Photo: SB Sports Photography


Mascot's Ash & Summer

#Sheffield17 posted a photo:

Mascot's Ash & Summer

Spectators at the Football Special Olympics at Sheffield and Hallam University Sports Park, Sheffield, UK. 09/08/2017 . Photo: SB Sports Photography