PHP 阅读 XML 播客 RSS 提要 [英] PHP Read XML Podcast RSS Feed

查看:49
本文介绍了PHP 阅读 XML 播客 RSS 提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 提要的方式(这是我的一个节目 - 用于测试目的).

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

点击查看我的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>';
    }
?>

问题是 - 我不知道如何从 enclosure 节点的属性 url 中获取信息,以便我可以在页面上显示它其余信息(这将在我制作播放器时派上用场 - 最终).

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).

所以!如何从 enclosure 节点获取 url 属性?我的意思是不是全错了?

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.

推荐答案

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

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

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

但这是从 XML DOM 获取节点和值的另一种更舒适的方法:使用 Xpath.请参阅此答案:https://stackoverflow.com/a/20225186/2265374

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

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

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.

您也可以通过这种方式直接获取属性.像enclosure元素的url属性:

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";

这篇关于PHP 阅读 XML 播客 RSS 提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆