Page 1 of 1

parsing media RSS in php 5

Posted: Tue May 29, 2007 6:28 pm
by psm9640
Hey folks,

I'm new to php 5 and this board so please forgive my being a newbie...

I have a media RSS feed from Brightcove that is formatted as such (I've cut it down for viewing purposes but there are 152 items similar to this one):

Code: Select all

<rss version="2.0" xmlns:bc="http://www.brightcove.com/link" xmlns:media="http://search.yahoo.com/mrss/">
    <channel>
        <item>
            <title>2006 Farm Aid</title>
            <link>http://link.brightcove.com/services/link/bcpid219243171/bclid232321226/bctid894795069?src=mrss</link>
            <description>There are many people who make Farm Aid a success every year, we meet some of them and show clips of Willie Nelson, Neil Young, Dave Matthews, and John Mellencamp performing.</description>
            <guid>http://link.brightcove.com/services/link/bcpid219243171/bclid232321226/bctid894795069?src=mrss</guid>
            <pubDate>Mon, 14 May 2007 08:01:27 -0700</pubDate>

            <media:content medium="video" type="video/x-flv" url="http://brightcove.vo.llnwd.net/d3/unsecured/media/2150793/2150793_893937669_8538c8df441bc0db0b98116d538f154b99a8ecfb.flv"/>
            <media:player height="606" url="http://link.brightcove.com/services/link/bcpid219243171/bclid232321226/bctid894795069?src=mrss" width="917"/>
            <media:thumbnail url="http://brightcove.vo.llnwd.net/d3/unsecured/media/2150793/2150793_894666376_ad6e1e3dae259a654303498d9c3d52787c5a9e65.jpg"/>
            <bc:playerid>219243171</bc:playerid>
            <bc:lineupid>232321226</bc:lineupid>
            <bc:titleid>894795069</bc:titleid>
        </item>
    </channel>
</rss>
Trying out simplexml, I'm easily able to grab the standard item elements such as title, link and description:

Code: Select all

$feedUrl = 'http://link.brightcove.com/services/link/bcpid219243171?action=mrss'; 
$rawFeed = file_get_contents($feedUrl); 
$xml = new SimpleXmlElement($rawFeed);

foreach ($xml->channel->item as $item) 
{     

    $title = $item->title;
    $link = $item->link;
    $description = $item->description;
     
    
//
echo "<p><a href=\"$link\">$title</a><br>";
echo "Description: $description";
//
    
}
But when I try to get more info from the feed, I'm running into 2 problems:

1. The semicolon in the Brightcove codes:

ex: <bc:titleid>894795069</bc:titleid>

Code: Select all

$feedUrl = 'http://link.brightcove.com/services/link/bcpid219243171?action=mrss'; 
$rawFeed = file_get_contents($feedUrl); 
$xml = new SimpleXmlElement($rawFeed);

foreach ($xml->channel->item as $item) 
{     

    $title = $item->title;
    $link = $item->link;
	$description = $item->description;
	$titleId = $item->bc:titleid;
     
    
//
echo "<p>Title ID: $titleId<br>";
echo "<a href=\"$link\">$title</a><br>";
echo "Description: $description";
//
    
}
2. And the url attribute (the address to the actual thumbnail):

ex: <media:thumbnail url="http://brightcove.vo.llnwd.net/d3/unsec ... 9e65.jpg"/>

I guess I am wondering 1: how do I escape/get around the colon in my first question, and 2: how do I grab that URL attribute from the media:thumbnail element?

If anyone could point me in the right direction I would really appreciate it. Thanks in advance!

-PM

Posted: Tue May 29, 2007 7:05 pm
by RobertGonzalez
Have you looked at Magpie or SimplePie?

Posted: Tue May 29, 2007 8:29 pm
by psm9640
Yeah, I've tried some things with simplepie but I'm not the most talented PHP programmer so it's kind of leaving me with more questions than answers. I'm going to keep plugging away but any help would be great.

Posted: Tue May 29, 2007 10:57 pm
by Kieran Huggins
the thing that's messing you up is called "namespaces" - apparently you can use simpleXML and namespaces, but you have to query the simpleXML object with an xpath selector to do it.

check out http://google.com/search?q=php+simplexml+namespace for more info

Posted: Wed May 30, 2007 11:08 am
by psm9640
Been checking that stuff out too...I'm lost -- everything I try hits a dead-end and it's just not worth the frustration...I've got a guy who I can hire to figure it out....thanks anyway!

Posted: Wed May 30, 2007 1:15 pm
by Ollie Saunders
DOM also supports namespaces natively. Check out a tutorial for DOM.

Posted: Wed May 30, 2007 2:23 pm
by volka
SimpleXML::children() also supports namespaces.

Code: Select all

<?php
$xml = <<< eox
<rss version="2.0" xmlns:bc="http://www.brightcove.com/link" xmlns:media="http://search.yahoo.com/mrss/">
    <channel>
        <item>
            <title>2006 Farm Aid</title>
            <link>http://link.brightcove.com/services/link/bcpid219243171/bclid232321226/bctid894795069?src=mrss</link>
            <description>There are many people who make Farm Aid a success every year, we meet some of them and show clips of Willie Nelson, Neil Young, Dave Matthews, and John Mellencamp performing.</description>
            <guid>http://link.brightcove.com/services/link/bcpid219243171/bclid232321226/bctid894795069?src=mrss</guid>
            <pubDate>Mon, 14 May 2007 08:01:27 -0700</pubDate>

            <media:content medium="video" type="video/x-flv" url="http://brightcove.vo.llnwd.net/d3/unsecured/media/2150793/2150793_893937669_8538c8df441bc0db0b98116d538f154b99a8ecfb.flv"/>
            <media:player height="606" url="http://link.brightcove.com/services/link/bcpid219243171/bclid232321226/bctid894795069?src=mrss" width="917"/>
            <media:thumbnail url="http://brightcove.vo.llnwd.net/d3/unsecured/media/2150793/2150793_894666376_ad6e1e3dae259a654303498d9c3d52787c5a9e65.jpg"/>
            <bc:playerid>219243171</bc:playerid>
            <bc:lineupid>232321226</bc:lineupid>
            <bc:titleid>894795069</bc:titleid>
        </item>
    </channel>
</rss>
eox;

$rss = simplexml_load_string($xml);

foreach($rss->channel->item->children('media', true) as $media) {
	echo 'media: ', $media->getName(), "<br />\n";
}

foreach($rss->channel->item->children('bc', true) as $bc) {
	echo 'bc: ', $bc->getName(), "<br />\n";
}

Posted: Wed May 30, 2007 3:56 pm
by psm9640
WOW -- thanks for the response volka -- now, how do I actually print/echo out those attributes as variables? I tried using attributes() just to see if I could make something happen but what was happening I have no idea! I just want to be able to access the different attributes for the media elements (url, width, height).

Code: Select all

<?php

 
$rss = simplexml_load_file('http://link.brightcove.com/services/link/bcpid219243171?action=mrss'); 

foreach($rss->channel->item as $item) {
	        echo '<p>title: ', $item->title, "<br />\n";

foreach($rss->channel->item->children('media', true) as $media) { 
        echo 'media: ', $media->getName(), "<br />\n"; 
        echo 'file: ' , $media->attributes(), "<br />n";
} 

foreach($rss->channel->item->children('bc', true) as $bc) { 
        echo 'bc: ', $bc->getName(), "<br />\n"; 
}

}


?>

Posted: Thu May 31, 2007 3:17 pm
by psm9640
Success!

Thanks for getting me started down the right path volka...I was finally able to get all this sorted out and it's absolutely amazing how much less code there is for parsing this feed than if I were using PHP 4:

Code: Select all

<?php


 
$rss = simplexml_load_file('http://link.brightcove.com/services/link/bcpid219243171?action=mrss'); 

foreach($rss->channel->item as $item) {
	        echo '<p>title: ', $item->title, "<br />\n";
	        echo 'link: ', $item->link, "<br />\n";
	        echo 'description: ', $item->description, "<br />\n";

foreach($item->children('media', true) as $media) {

	foreach ($media->attributes() as $key => $value) {
	
	echo $media->getName(), " {$key}: {$value}<br>";
}

} 

foreach($item->children('bc', true) as $bc) { 
        echo 'bc: ', $bc->getName(), ": ",$bc, "<br />\n"; 

}

}


?>