【发布时间】:2011-06-29 10:17:59
【问题描述】:
我用一个很普通的php脚本来解析RSS;
function getRSS($source,$quien) {
$start = microtime(true);
ini_set('default_socket_timeout', 1);
global $arrFeeds, $downItems, $time_taken;
$arrFeeds = array();
$ch = curl_init($source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
$doc = new DOMDocument();
$doc->loadXML($content);
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => sistema($node->getElementsByTagName('title')->item(0)->nodeValue),
'desc' => sistema($node->getElementsByTagName('description')->item(0)->nodeValue),
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue
);
echo(sistema($node->getElementsByTagName('title')->item(0)->nodeValue)."<br>");
array_push($arrFeeds, $itemRSS);
$downItems+=1;
}
$time_taken = microtime(true) - $start;
if ($downItems>1) {$nu=mysql_query("UPDATE feeds SET lastcheck = NOW() WHERE id = '".$quien."';");}
}
当我用谷歌的rss news feed 测试这段代码时,它工作得很好,但如果我用这个other rss feed 尝试它,它就不会工作,并给出了很多* 之类的错误; 警告:DOMDocument::loadXML() [domdocument.loadxml]:开始和结束标签不匹配:P 第 5 行和实体中的 BODY,第 6 行在 C:\Users\Domingo\Dropbox\www\temp\parser。 php 在第 18 行。上面两个 rss 文件是有效的,我发现的唯一不同是其中一个有以下行:<?xml version="1.0" encoding="utf-8"?>,另一个没有。这是问题吗?我该如何解决这个问题?感谢您的帮助,请不要建议使用 rss 解析器库。
(*) 更多错误,例如:Warning: DOMDocument::loadXML() [domdocument.loadxml]: Opening and ending tag mismatch: BODY line 3 and HTML in Entity, line: 6 in C:\Users\Domingo\Dropbox\www\temp\parser.php on line 18
和Warning: DOMDocument::loadXML() [domdocument.loadxml]: Premature end of data in tag HTML line 1 in Entity, line: 7 in C:\Users\Domingo\Dropbox\www\temp\parser.php on line 18
【问题讨论】:
-
您的代码不会独立运行,因此其他人很难调试它。我建议您编写一个简化版本并用它更新问题。
-
解析器抱怨在提要中不存在的 XML 元素 P 和 BODY。也许脚本检索到的数据与浏览器检索到的数据不同。你确定脚本有要解析的 XML 吗?顺便说一句,来自 W3.org 的提要验证器不认为第二个提要有效。
标签: php rss xml-parsing