Parsing a Flickr RSS feed requires some preparatory steps. First of all, I tried to download a static copy of my feeds using wget just to study the file structure. Wrong choice! In fact, the format returned was Atom, not RSS. So I used the following approach:
$file = file_get_contents('http://api.flickr.com/services/feeds/photos_public.gne?id=31968388@N02&lang=it-it&format=rss_200');
echo $file;
So I got the correct format. Parsing the feed is quite a simple task:
$rss = simplexml_load_file('http://api.flickr.com/services/feeds/photos_public.gne?id=31968388@N02&lang=it-it&format=rss_200');
foreach($rss->channel->item as $entry) {
$title = $entry->title;
$raw_published = $entry->pubDate;
$published = str_replace('-0700', '', $raw_published);
$raw_content = $entry->description;
$content = html_entity_decode($raw_content, UTF-8);
echo '<li>' . "\n" . '<h2>' . $title . '</h2>' . "\n" . '<p class="pubdate">' . $published . "</p>\n" .
$content . "</li>\n";
}
Two notes here:
- you need to start parsing from the root element, not from the
SimpleXMLObjectitself - the
descriptionelement contains markup that needs to be expanded using thehtml_entity_decode()function
You can see this test here.