我正在尝试解析某些媒体的RSS提要。我的脚本适用于大多数脚本。问题是,即使它们格式不正确,我也需要汇总它们。
我无法获得这两个提要的描述。无论如何,我该如何进行?
这是我的脚本:
<?php
function RSS_items ($url) {
$i = 0;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName('channel');
foreach($channels as $channel) {
$items = $channel->getElementsByTagName('item');
foreach($items as $item) {
$i++;
$y[$i]['title'] = $item->getElementsByTagName('title')->item(0)->firstChild->textContent;
$y[$i]['link'] = $item->getElementsByTagName('link')->item(0)->firstChild->textContent;
$y[$i]['updated'] = $item->getElementsByTagName('pubDate')->item(0)->firstChild->textContent;
$y[$i]['description'] = $item->getElementsByTagName('description')->item(0)->firstChild->textContent;
}
}
echo '<pre>';
print_r ($y);
echo '</pre>';
}
// the two malformed feeds
RSS_items ('http://www.lefigaro.fr/rss/figaro_actualites-a-la-une.xml');
RSS_items ('https://francais.rt.com/rss');
?>
您代码的问题是在使用firstChild
选择元素第一个子元素的属性中。但是在目标XML中,description
标记没有您要首先选择的子代。从代码中将其删除。结果应该是这样的
$item->getElementsByTagName('description')->item(0)->textContent;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句