PHP阅读XML播客RSS提要

PHP阅读XML播客RSS提要,php,xml,rss,Php,Xml,Rss,好的,我正在为一个朋友的播客网站创建一个页面,列出他播客上的所有剧集。从本质上讲,我要寻找的就是如何阅读RSS提要。解析出节点,并在屏幕上显示信息。最后,我将创建一个播放器来播放这些剧集,但那要晚得多 这就是我阅读RSS提要的方式,它是我的一个节目的RSS提要,用于测试目的 点击查看 问题是——我不知道如何从封闭节点的属性url中获取信息,这样我就可以在页面上显示它和其他信息——这将在我制作播放器时派上用场——最终 所以!如何从机柜节点获取url属性?我这样做完全错了吗 如有任何有用的提示,将不

好的,我正在为一个朋友的播客网站创建一个页面,列出他播客上的所有剧集。从本质上讲,我要寻找的就是如何阅读RSS提要。解析出节点,并在屏幕上显示信息。最后,我将创建一个播放器来播放这些剧集,但那要晚得多

这就是我阅读RSS提要的方式,它是我的一个节目的RSS提要,用于测试目的

点击查看

问题是——我不知道如何从封闭节点的属性url中获取信息,这样我就可以在页面上显示它和其他信息——这将在我制作播放器时派上用场——最终

所以!如何从机柜节点获取url属性?我这样做完全错了吗


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

如果您决定在本文中使用DOMDocument,我深表歉意,但由于到目前为止还没有人给出答案……下面是一个使用简单的xml加载文件的脚本,我发现它很容易掌握

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






    ?>
节点有一个getAttribute方法。因此,您可以使用:

$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')
但这里有另一种从XMLDOM获取节点和值的更舒适的方法:使用Xpath。看看这个答案:

如果找不到与SimpleXML btw相同的元素,$node->getElementsByTagName'enclosure'->item0将导致错误。如果在Xpath中将节点列表转换为字符串,则结果只是一个空字符串,不会触发错误

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

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

这不完全是我想要的,但我可以看到这对我一直在绞尽脑汁的另一个项目很有用。非常感谢。此方法似乎不适用于获取文件URL或iTunes->duration标记(如果存在)的附件。
echo 'Enclosure Url: ', $xpath->evaluate('string(enclosure/@url)', $rssItem), "\n";