PHP阅读XML播客RSS Feed

时间:2023-02-12 01:56:00

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)

好的,所以,我正在为朋友的播客网站创建一个页面,列出他的播客的所有剧集。基本上,我正在寻找的是如何阅读RSS Feed。解析节点,并在屏幕上显示信息。 (最终,我将创建一个播放剧集的播放器,但那要晚得多)

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

这就是我正在阅读RSS Feed(这是我的一个节目 - 用于测试目的)。

click to see My Feed

点击查看我的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).

问题是 - 我不知道如何从机箱节点的属性URL中获取信息,这样我就可以在页面上显示其余的信息(当我制作播放器时这会派上用场 - 最终)。

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

所以!如何从机箱节点获取url属性?我错了吗?

Any helpful hints would be appreciated. Thanks.

任何有用的提示将不胜感激。谢谢。

2 个解决方案

#1


1  

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

节点有一个getAttribute()方法。所以你可以使用:

$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://*.com/a/20225186/2265374

但是,从XML DOM中获取节点和值是另一种更舒适的方法:使用Xpath。请参阅此答案:https://*.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.

如果没有找到元素,则$ node-> getElementsByTagName('enclosure') - > item(0)将导致错误(对于SimpleXML btw也是如此)。如果节点列表在Xpath中强制转换为字符串,则结果只是一个空字符串,并且不会触发错误。

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

您也可以通过这种方式直接获取属性。与enclosure元素的url属性一样:

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

#2


2  

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.

抱歉,如果你决定在这里使用DOMDocument(),但是因为到目前为止没有人发布过答案......这里是一个使用simple_xml_load_file()的脚本,我发现很容易掌握它。

    <?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++    )    ?>

#1


1  

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

节点有一个getAttribute()方法。所以你可以使用:

$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://*.com/a/20225186/2265374

但是,从XML DOM中获取节点和值是另一种更舒适的方法:使用Xpath。请参阅此答案:https://*.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.

如果没有找到元素,则$ node-> getElementsByTagName('enclosure') - > item(0)将导致错误(对于SimpleXML btw也是如此)。如果节点列表在Xpath中强制转换为字符串,则结果只是一个空字符串,并且不会触发错误。

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

您也可以通过这种方式直接获取属性。与enclosure元素的url属性一样:

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

#2


2  

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.

抱歉,如果你决定在这里使用DOMDocument(),但是因为到目前为止没有人发布过答案......这里是一个使用simple_xml_load_file()的脚本,我发现很容易掌握它。

    <?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++    )    ?>