Pergunta

OK, so, I'm creating a page for a friend's podcast site that lists out all of the episodes to his podcast(s). Essentially, all I'm looking for is how to read the RSS Feed. Parse out the Nodes, and display the information on the screen. (eventually, I'm going to create a player that will play the episodes, but that's much later)

This is how I'm reading the RSS Feed (which is to one of my shows - for testing purposes).

click to see My Feed

<?php

    //Errors:
    ini_set('display_errors', 'On');
    error_reporting(E_ALL);

    $rss = new DOMDocument();
    $rss->load('http://tbpc.podbean.com/feed/');
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue,
            'enclosure' => $node->getElementsByTagName('enclosure')->item(0)->nodeValue,
            );
        array_push($feed, $item);
    }
    $limit = 1;
    for($x=0;$x<$limit;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
        $description = $feed[$x]['desc'];
        $short =  substr($description, 0, strpos( $description, '&lt;'));
        $file = $feed[$x]['guid'];
        echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong></p>';
        echo '<p>'.$description.'</p>';
        echo '<p>'.$short.'</p>';
        echo '<p>'.$file.'</p>';
    }
?>

The problem is - is that I have no idea how to get the information out of the attribute url of the enclosure node so I can display it on the page with the rest of the information (this will come in handy when I make the player - eventually).

SO! How do I get the url attribute from the enclosure node? Am I going about this all wrong?

Any helpful hints would be appreciated. Thanks.

Foi útil?

Solução 2

Nodes have an getAttribute() method. So you can use:

$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')

But here is another and more comfortable way to fetch nodes and values from an XML DOM: Use Xpath. See this answer: https://stackoverflow.com/a/20225186/2265374

The $node->getElementsByTagName('enclosure')->item(0) will result in an error if no element is found (same goes for SimpleXML btw). If the node list is cast to string in Xpath, the result is just an empty string and no error is triggered.

You can directly fetch attributes this way, too. Like the url attribute of the enclosure element:

echo 'Enclosure Url: ', $xpath->evaluate('string(enclosure/@url)', $rssItem), "\n";

Outras dicas

Apologies if you're determined to use DOMDocument() in this, but since nobody has posted an answer so far...here's a script which uses simple_xml_load_file(), which I found quite easy to get to grips with.

    <?php

        $rss_array = array('http://rss.computerworld.com/computerworld/s/feed/topic/231', 'http://rss.computerworld.com/computerworld/s/feed/topic/230', 'http://rss.computerworld.com/computerworld/s/feed/topic/66', 'http://www.engadget.com/rss.xml', 'http://feeds.webservice.techradar.com/rss/new', 'http://feeds.arstechnica.com/arstechnica/index', 'http://www.notebookcheck.net/News.152.100.html', 'http://electronista.feedsportal.com/c/34342/f/626172/index.rss', 'http://www.anandtech.com/rss/pipeline/', 'http://www.digitimes.com/rss/daily.xml', 'http://feeds.feedburner.com/TechCrunch/', 'http://feeds2.feedburner.com/ziffdavis/pcmag/breakingnews', 'http://feeds.feedburner.com/Liliputing', 'http://feeds.slashgear.com/slashgear', 'http://feeds.feedburner.com/GizmagEmergingTechnologyMagazine', 'http://www.zdnet.com/news/rss.xml', 'http://feeds.feedburner.com/mobilityupdate', 'http://www.techmeme.com/feed.xml', 'http://www.notebookreview.com/rss.xml'); 

    for ($i=0; $i<count($rss_array); $i++ ) {
                $rssfeed = simplexml_load_file($rss_array[$i]);
                foreach ($rssfeed->channel as $channel) {

                    echo '<h1>' . htmlentities($channel->title) . '</h1>';
                    echo '<p>' . htmlentities($channel->description) . '</p>';
                    echo '<p><a href="' . htmlentities($channel->link) . '">' .
                        htmlentities($channel->link) . '</a></p>';

                    echo '<input type="button" value="  >>>  " onClick="downloadFileViaAjax(\'' . htmlentities($channel->link) . '\')">';     



                    echo '<ul>';
                    foreach ($channel->item as $item) {
                        echo '<li><a href="' . htmlentities($item->link) . '">';
                        echo htmlentities($item->title) . '</a>';
                    //  echo htmlentities($item->description) . '</li>';
                        echo '<input type="button" value="  >>>  " onClick="downloadFileViaAjax(\'' . htmlentities($item->link) . '\')"></li>';   

                    }
                    echo '</ul>';
                }
    }//fur ( $rss_array++    )






    ?>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top