Monday, July 6, 2009

XML - Parse RSS media:thumbnail with simpleXML

PHP 5's simpleXML treats an RSS media:thumbnail entry as if it were a namespace. simpleXML doesn't have a built in fix for this, so I had to create my own.

I won't share the rest of the secrets of that project with you, but feel free to use my code on your own project.


<!-- For RSS media feeds of the following format: ( channel/item/{title, description, link, GUID, media:thumnail[url]} ) -->

// I wrote this to accept a POST or GET from a webform
// You can also send the 'xmlurl' variable like this:
$xmlURL = $_REQUEST('xmlurl');

$entries = new SimpleXMLElement('$xmlURL', null, true);

foreach ($entries->channel->item as $item)
foreach ($item->xpath ('media:thumbnail') as $mediathumbnail) {
$thumb = $mediathumbnail['url'];
foreach ($item->children() as $child)
switch ($child->getName())
case 'title':
$title = $child;

case 'link':
$link = $child;

case 'description':
$description = $child;

echo "";
echo "<tr><td><a href='".$link."' target='_blank'><img src='".$thumb."'></a></td>";
echo "<td><a href='".$link."' target='_blank'>".$title."</a><br>".$description."</td><tr>";
echo "";

1 comment:

Djangofan said...

hey my brother. just noticed that you have a blogger blog just like me. go into your Blogger dashboard and add my site to the list of sites you are following!