SimplePie 1.5 is now available!

SimplePie Documentation.  Learn how to use this thing.  It's way better than going to school.

You are here: Documentation » Tips, Tricks, Tutorials, and Screencasts » Grab custom tags or attributes

Grab custom tags or attributes

SimplePie 1.0 introduces the ability to grab data from any element in the feed, even if there aren't public methods for grabbing them. This is a popular feature among RSS hackers.

SimplePie maps data by the namespace URL, and from there you can use PHP's print_r() function to navigate the rest of the way through the array. This example shows you how to get an event's start date from a Google Calendar feed. Not only will we find this date data, but we'll reformat it and then sort the items chronologically based on it.

We'll use the get_item_tags() in this example, although we could also use get_channel_tags() for the feed-level data.

:!: This tutorial assumes that you're already familiar with using SimplePie, including looping through items. This is only sample code, and you should not create real pages using the (horrid) HTML generated by this example.

Compatibility

  • Supported in SimplePie 1.0.
  • Code in this tutorial should be compatible with PHP 4.3 or newer, and should not use PHP short tags, in order to support the largest number of PHP installations.

Code source

<?php
// Make sure that SimplePie is loaded
require_once('../simplepie.inc');
 
// Let's create a new SimplePie object
$feed = new SimplePie();
 
// This is the feed we'll use
$feed->set_feed_url('http://www.google.com/calendar/feeds/eventi%40emmealcubo.com/public/full');
 
// Let's turn this off because we're just going to re-sort anyways, and there's no reason to waste CPU doing it twice.
$feed->enable_order_by_date(false);
 
// Initialize the feed so that we can use it.
$feed->init();
 
// Make sure the content is being served out to the browser properly.
$feed->handle_content_type();
 
// We'll use this for re-sorting the items based on the new date.
$temp = array();
 
foreach ($feed->get_items() as $item) {
 
	// We want to grab the Google-namespaced <gd:when> tag.
	$when = $item->get_item_tags('http://schemas.google.com/g/2005', 'when');
 
	// Once we grab the tag, let's grab the startTime attribute
	$date = $when[0]['attribs']['']['startTime'];
 
	// Let's convert it all to UNIX timestamp. This will be used for sorting.
	$sortDate = SimplePie_Misc::parse_date($date);
 
	// Let's format it with date(). This will be the date we display.
	$gCalDate = date('j M Y', $sortDate);
 
	// This is how each item will be displayed. We're adding it to the array we created earlier, and indexing it by the $sortDate.
	$temp[$sortDate] = '<p>' . $item->get_title() . ' <small> &mdash; ' . $gCalDate . '</small></p>';
}
 
// Change this to krsort() to display with the furthest event first.
ksort($temp);
 
// Loop through the (now sorted) array, and display what we wanted.
foreach ($temp as $paragraph) {
	echo $paragraph;
}
?>

Notes

If there is a tag in the feed that does not have a namespace (i.e. <thumb> instead of <media:thumbnail>), use an empty string for the $namespace parameter.


tutorial/grab_custom_tags_or_attributes.txt · Last modified: 2011/03/06 03:56 (external edit)