<?xml version="1.0"?>
<rss version="2.0"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:pbscontent="http://www.pbs.org/rss/pbscontent/"
xmlns:pbsvideo="http://www.pbs.org/rss/pbsvideo/" >
<channel>
<title>The Local Show | PBS Video</title>
<description>The Local Show RSS feed for PBS programming.</description>
<link>http://video.pbs.org</link><language>en-us</language>
<generator>http://video.pbs.org</generator>
<lastBuildDate>Fri, 15 Mar 2013 10:16:35 -0400</lastBuildDate>
<pubDate>Fri, 15 Mar 2013 10:16:35 -0400</pubDate>
<item>
<title>The Local Show | KC Makers, Celebrating Extraordinary Women</title>
<link>http://video.pbs.org/video/2338801013/</link>
<description>This week, we celebrate the achievements of just a few of the extraordinary women who live in the Metro.</description>
<guid>http://video.pbs.org/video/2338801013/</guid>
<pubDate>02/25/2013</pubDate>
<media:description>The Local Show celebrates Kansas City's Makers: Women Who Make America.</media:description>
<media:content medium="video" duration="1611000" />
<media:thumbnail url="http://pbs.merlin.cdn.prod.s3.amazonaws.com/Video%20Asset/KCPT/local- show/70943/images/567745_ThumbnailCOVEDefault_20130225174212.jpg.resize.142x80.jpg"
type="image/jpeg" height="60" width="142" />
<media:rating scheme="urn:v-chip">nr</media:rating>
<media:player url="http://video.pbs.org/video/2338801013/" />
<category domain="PBS/taxonomy/topic">Arts & Entertainment</category>
<media:category scheme="http://www.pbs.org/rss/pbscontent/taxonomy/topic">Arts & Entertainment</media:category>
<category domain="PBS/taxonomy/topic">Culture & Society</category>
<media:category scheme="http://www.pbs.org/rss/pbscontent/taxonomy/topic">Culture & Society</media:category>
<category domain="PBS/taxonomy/topic">Health</category>
<media:category scheme="http://www.pbs.org/rss/pbscontent/taxonomy/topic">Health</media:category>
<category domain="PBS/taxonomy/topic">News & Public Affairs</category>
<media:category scheme="http://www.pbs.org/rss/pbscontent/taxonomy/topic">News & Public Affairs</media:category>
<category domain="PBS/taxonomy/topic">Parents</category>
<media:category scheme="http://www.pbs.org/rss/pbscontent/taxonomy/topic">Parents</media:category><category domain="PBS/taxonomy/topic">Technology</category><media:category scheme="http://www.pbs.org/rss/pbscontent/taxonomy/topic">Technology</media:category>
<pbsvideo:content_type>Episode</pbsvideo:content_type>
</item>
[...Many more items]
</channel>
</rss>
<media:description></media:description>
The Local Show celebrates Kansas City's Makers: Women Who Make America.
<media:content medium="video" duration="1611000" />
These are not read the same way as values, but can be read using a standard library method, which I will cover later. As for the code, let us start at the beginning. Get the url, store the contents in a variable using file_get_contents then convert them to an xml resource using simplexml_load_string.
$url = "http://video.pbs.org/program/local-show/rss/";
$contents = file_get_contents($url);
$xml = simplexml_load_string($contents);
The manual says you can do a straight load string from the url, but I prefer this method. It has no overhead and is easier to debug, should I have to. This xml is a well formed RSS and consists of the tags <channel> and <item>. To read the data within them we use nested foreach constructs
foreach ($xml->channel as $channel)
{
foreach ($channel->item as $item)
{
foreach ($item as $feed)
{
echo "The feed : ". $feed. "<br />";
}
}
}
This will echo all the data in the standard tags but none of namespace tags or any of the attributes. To get to the namespace media: we use the getNamspaces() and the children() methods
foreach ($xml->channel as $channel)
{
foreach ($channel->item as $item)
{
$ns = $item->getNamespaces(true); //Apply method to <item> tag
$child = $item->children($ns["media"]); //Extract the “media:” namespace
foreach ($item as $feed)
{
echo "The feed : ". $feed. "<br />";
}
}
}
Then add an extra foreach to handle the new namespace variables
foreach ($xml->channel as $channel)
{
foreach ($channel->item as $item)
{
$ns = $item->getNamespaces(true);
$child = $item->children($ns["media"]);
foreach ($item as $feed)
{
echo "The feed : ". $feed. "<br />";
}
foreach ($child as $name)
{
echo "the name space : ".$name ."<br />"; //Output namespace values
}
}
}
This will echo the values of the namespace tags. Some of the tags, however, contain no data and will echo blank lines to the screen. But they do have attributes. These can be accessed with the attributes() method, and they have to be accessed directly. For example to get to the attributes of the namespace <media:content> we use this foreach statement on the previously populated variable $child addressing the content value
foreach ($child->content->attributes() as $attrib_name => $attrib_value )
{
echo "the attribute name : ".$attrib_name." the attribute value : ".$attrib_value;
}
echo "medium : ".$child->content->attributes()->medium;
$cats = $item->category->count();
for ($i = 0; $i < $cats; $i++)
{
echo $item->category[$i];
echo $child->category[$i]->attributes()->scheme;
}
<?php
$url = "http://video.pbs.org/program/local-show/rss/";
$contents = file_get_contents($url);
$xml = simplexml_load_string($contents);
foreach ($xml->channel as $channel)
{
foreach ($channel->item as $item)
{
$ns = $item->getNamespaces(true);
$child = $item->children($ns["media"]);
echo "Programme Title : " . $item->title . "<br />";
echo "Video Link : " . $item->link . "<br />";
echo "Description : " . $item->description . "<br />";
echo "Transmitted : " . $item->pubDate . "<br />";
$cats = $item->category->count();
echo "Found in the following categories :<br />";
for ($i = 0; $i < $cats; $i++)
{
echo $item->category[$i];
echo " link : ". $child->category[$i]->attributes()->scheme . "<br/>";
}
echo "Rating: " . $child->rating . "<br/>";
//Calculate the time from milliseconds
$x = $child->content->attributes()->duration /(60 * 1000);
$m = floor($x);
$s = number_format(($x - $m) * 60);
echo "Duration : $m minutes $s seconds <br/><br/><br/>";
}
}
?>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented:
Thank you very much for your effort
I asked them to generate a smaller file for me.
Here is a full one: https://dl.dropbox.com/u/33313692/CodesAndDescription.xml