好的,所以,我正在为朋友的播客网站创建一个页面,列出他的播客的所有剧集.基本上,我正在寻找的是如何阅读RSS Feed.解析节点,并在屏幕上显示信息. (最终,我将创建一个能够播放剧集的播放器,但那要晚得多)
这就是我正在阅读RSS Feed(这是我的一个节目 – 用于测试目的).
点击查看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(' & ', ' & ', $Feed[$x]['title']);
$link = $Feed[$x]['link'];
$description = $Feed[$x]['desc'];
$short = substr($description, 0, strpos( $description, '<'));
$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>';
}
?>
问题是 – 我不知道如何从机箱节点的属性URL中获取信息,这样我就可以在页面上显示其余的信息(当我制作播放器时这会派上用场 – 最终).
任何有用的提示将不胜感激.谢谢.
解决方法:
节点有一个getAttribute()方法.所以你可以使用:
$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')
但是,从XML DOM中获取节点和值是另一种更舒适的方法:使用Xpath.看到这个答案:https://stackoverflow.com/a/20225186/2265374
如果没有找到任何元素,则$node-> getElementsByTagName(‘enclosure’) – > item(0)将导致错误(对于SimpleXML btw也是如此).如果节点列表在Xpath中强制转换为字符串,则结果只是一个空字符串,并且不会触发错误.
您也可以通过这种方式直接获取属性.与enclosure元素的url属性一样:
echo 'Enclosure Url: ', $xpath->evaluate('string(enclosure/@url)', $RSSItem), "\n";
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。