How to parse the <enclosure> tage in RSS feed?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

How to parse the <enclosure> tage in RSS feed?

Post by becky-atlanta »

I found this code to help me get started in parsing RSS feed and extract the basic information I wanted. However, I have tried and tried and tried to parse the <enclosure> tag to extract the url, length and type but no success. What do you suggest I should add to the code below to get the enclosure information?

Code: Select all

    $doc = new DOMDocument();
    $doc->load('http://davebender.podbean.com/feed/');
    $arrFeeds = array();
    foreach ($doc->getElementsByTagName('item') as $node) {
        $itemRSS = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
            );
        array_push($arrFeeds, $itemRSS);
 
I appreciate your help.
Last edited by Benjamin on Wed Jun 10, 2009 11:55 am, edited 1 time in total.
Reason: Changed code type from text to php.
RishikeshJha
Forum Newbie
Posts: 11
Joined: Tue Mar 31, 2009 1:54 am

Re: How to parse the <enclosure> tage in RSS feed?

Post by RishikeshJha »

hi,
use this code in your code:
put this code at the start of the code...

Code: Select all

 
$myArray;
$index=-1;
put this code after your own code...

Code: Select all

$myArray[++$index]['title']=$itemRSS['title'];
        $myArray[$index]['desc']=$itemRSS['desc'];
        $myArray[$index]['link']=$itemRSS['link'];
        $myArray[$index]['date']=$itemRSS['date'];  
 
finally return $myArray
Last edited by Benjamin on Wed Jun 10, 2009 11:56 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to parse the <enclosure> tage in RSS feed?

Post by becky-atlanta »

I have no problem of getting those variables. But what I want from the feed is the <enclosure> tag which have three elements: url, length and type. I don't know how to extract the enclosure tag. Any idea?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: How to parse the <enclosure> tage in RSS feed?

Post by mikemike »

Without knowing the strucutre of the XML file...

$node->getElementsByTagName('enclosure')->item(0)->nodeValue; perhaps?
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to parse the <enclosure> tage in RSS feed?

Post by becky-atlanta »

I had tried that before I turned to the forum for help. It did not work.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to parse the <enclosure> tage in RSS feed?

Post by pickle »

Maybe use an RSS library like SimplePie?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to parse the <enclosure> tage in RSS feed?

Post by becky-atlanta »

I really like the script I found. It is all very simple and works great. I wish I knew how to modify it to extract the ENCLOSURE tag.

Thanks for mentioning SimplePie. I am checking it out.
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to parse the <enclosure> tage in RSS feed?

Post by becky-atlanta »

I am just testing the example file from simplepie. It got an error on line 13104 in simplepie.inc.
Post Reply