0

使用以下链接,我获得了视频的 RSS 提要
http://rss.cnn.com/services/podcasting/ac360/rss.xml

我需要使用 PHP 中的 DOMDocument() 获取此链接并在我的网站上显示视频。
如何从 RSS 提要中获取这些视频并在我的网站上显示?
这是我的代码

$xml=("rss.cnn.com/services/podcasting/ac360/rss.xml";);
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml); 
$channel=$xmlDoc->getElementsByTagName('channel')->item(0); 
for ($i=0; $i<=2; $i++) {      
    $item_title=$x->item($i)
                  ->getElementsByTagName('title')
                  ->item(0)
                  ->childNodes
                  ->it‌​em(0)
                  ->nodeValue;   
    $item_link=$x->item($i)
                 ->getElementsByTagName('link')
                 ->item(0)
                 ->childNodes
                 ->item‌​(0)
                 ->nodeValue;  
    $item_desc=$x->item($i)
                 ->getElementsByTagName('description')
                 ->item(0)
                 ->childNode‌​s
                 ->item(0)
                 ->nodeValue;
    echo ("<p><a href='" . $item_link. "'>" . $item_title . "</a>");
}
4

1 回答 1

1

视频 url 在<media:content>标签的url属性下,你可以使用 xpath 来抓取这样的:

$xml=("rss.xml");
$doc = new DOMDocument();
$doc->load($xml);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//item') as $item) {
    $media = $xpath->query('//media:content', $item);
    $title = $xpath->query('//title', $item);
    print '<a href="'.$media->item(0)->getAttribute('url').'">'.$title->item(0)->textContent."</a>\n";
}

还可以考虑使用像simplepie这样的rss 解析库

于 2012-09-26T14:43:45.123 回答