Parse iTunes metadata to display in webpage

OK so I have very limited PHP coding knowledge and what I have been able to put together I usually find from various sources online and more-or-less manipulate that code to match what I’m trying to do. Case in point I needed to pull a list of items from an iTunes podcast feed and display into a table format on a webpage. I was able to find the basic code to pull most of the elements I needed and was able to manipulate that in order to get it to display in an table. My problem is whenever I want to pull an element that has the itunes: prefix. I receive no data from the request. So I’m not sue what I’m doing wrong.

So in the following code everything works exactly how I want it to except the lines where I reference ‘itunes:duration’, ‘dura’ and $duration. Currently that column is showing as empty no data at all however the feed does in fact contain that element.

Can anyone fill me in on what I’m missing here?

[php]

<?php $rss = new DOMDocument(); $rss->load('http://tulsachurchofgod.sermon.tv/rss/TCOG_2012_Archives/audio'); $feed = array(); $limit = 0; foreach ($rss->getElementsByTagName('item') as $node) { $limit = $limit+1; $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'creat' => $node->getElementsByTagName('creator')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('guid')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, 'dura' => $node->getElementsByTagName('itunes:duration')->item(0)->nodeValue, ); array_push($feed, $item); } echo '

'.$title.'
'; ?>

<? for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $creator = $feed[$x]['creat']; $date = date('m-d', strtotime($feed[$x]['date'])); $duration = $feed[$x]['dura']; ?> <? }

?>

DATE SPEAKER TITLE TIME
<?=$date;?> <?=$creator;?> <? echo ''.$title.'';?> <?=$duration;?>
[/php]

It has something to do with the : because you cannot read dc:creator either.

Dude! I don’t know why I was being so stupid! I thought for sure I had already tried that but for what ever reason when I did it this morning it worked like it was supposed to.

Now I just need to email the folks that generate the XML and find out why some of the time codes are different from others. Some of them display like 49:11 while others display as 00:31:45.

Thanks for pointing out what I was totally unable to see on my own.

What was your solution?

Here is the format for size I pounded that out when I found a length code.
[php]‘size’ => $node->getElementsByTagName(‘enclosure’)->item(0)->attributes->getNamedItem(length)->value,

$size_raw = $feed[$x][‘size’];
$size = number_format($size_raw/1024/1024,2);

<?=$size." MB";?> [/php]

also you can eliminate the lines in your “headers” if you use instead of for table header it auto bolds.

I simply removed the itunes: part and just left it as:
[php]‘dura’ => $node->getElementsByTagName(‘duration’)->item(0)->nodeValue,[/php]

I realized that when you mentioned that dc:creator didn’t work either. I knew that I was seeing the creator field just fine. So I went back and looked at the code and XML and realized that while the XML called it dc:creator the code was only calling for creator. So I yanked the itunes: part out and it worked.

I think I must have had a spelling error previously when I was trying to call duration by itself. I do remember that that is how I started out on the code but it didn’t work and so then I copied and pasted the itunes:duration tag straight from the XML.

And sweet I will take a look at those other suggestion here in just a bit.

Here you go, I know I tired that at least once I must not have typed it my self >:( ![php]<?php
$url = ‘http://tulsachurchofgod.sermon.tv/rss/TCOG_2012_Archives/audio’;
$rss = new DOMDocument();
$rss -> load($url);
$feed = array();
$limit = 0;
foreach ($rss->getElementsByTagName(‘item’) as $node) {
$limit++;
$item = array (
‘title’ => $node->getElementsByTagName(‘title’)->item(0)->nodeValue,
‘creat’ => $node->getElementsByTagName(‘creator’)->item(0)->nodeValue,
‘link’ => $node->getElementsByTagName(‘guid’)->item(0)->nodeValue,
‘date’ => $node->getElementsByTagName(‘pubDate’)->item(0)->nodeValue,
‘dura’ => $node->getElementsByTagName(‘duration’)->item(0)->nodeValue,
‘size’ => $node->getElementsByTagName(‘enclosure’)->item(0)->attributes->getNamedItem(length)->value,
);
array_push($feed, $item);
}
echo ’

‘.$title.’
’;
?>

<? for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $creator = $feed[$x]['creat']; $date = date('m-d', strtotime($feed[$x]['date'])); $duration_raw = $feed[$x]['dura']; $duration = substr($duration_raw, -5); $size_raw = $feed[$x]['size']; $size = number_format($size_raw/1024/1024,2); ?> <? }

?>

DATE SPEAKER TITLE TIME SIZE
<?=$date;?> <?=$creator;?> <? echo ''.$title.'';?> <?=$duration;?> <?=$size." MB";?>
[/php]

Yep that code works like a charm but then I found some inconsistencies within the XML code itself such as on that particular XML just one instance where instead of 64:48 it had the duration set for 01:04:48 and so on the output it just displayed 04:48. But that isn’t that big of a deal. I’m working asking the guys who generate the code about the inconsistencies.

On another note but part of the same code. I’ve noticed that when it brings in the date field sometimes the date output is incorrect. It will be just a day off and it is always a day earlier. within the XML if the pubDate is Sat, 28 Jan 2012 01:00:00 -0500 for example the out put reads 01-27 if the pubDate is Sat, 28 Jan 2012 12:00:00 -0500 then the output is as expected 01-28. Is there something I can add to the PHP to correct the difference? I’m thinking it must have to with time zoning or something.

If I had to guess it’s a 24 hour time format so the 01:00 is (1AM GMT) which minus five hours puts you one day earlier. However, 12:00 GMT -05:00 only makes it 07:00 of the same day.

Yep that’s what I figured too. So I started messing with trying to force the time zone. Add this bit [php]date_default_timezone_set(‘UTC’);[/php] at the start of all the php and BAM! everything appears to match up now.

How did this get marked solved?

I marked it as solved as all the questions I had were answered. Was I not supposed to do that? I’m not a big user of forums so I am unfamiliar with the etiquette for when a solution is found.

I meant how would one go about marking a post solved not that this one is not solved.

Oh I think you may have to be the owner of a thread in order to see it but I saw a button at the bottom of the thread that reads ‘TOPIC IS SOLVED’ I clicked that and it put the check mark next to the thread.

Sponsor our Newsletter | Privacy Policy | Terms of Service