使用 PHP 脚本解析 RSS 的问题 [英] Problem parsing RSS with a PHP script

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

问题描述

我使用一个很常见的php脚本来解析RSS;

im using a very common php script to parse 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 新闻提要 来自谷歌它工作得很好,但如果我用这个 其他 rss 提要,它不起作用,并给出了很多错误,例如;警告:DOMDocument::loadXML() [domdocument.loadxml]:开始和结束标签不匹配:P 第 5 行和实体中的 BODY,C:\Users\Domingo\Dropbox\www\temp\parser 中的第 6 行.php 在第 18 行.上面两个 rss 文件是有效的,我发现它们之间唯一的区别是其中一个具有以下行:<?xml version="1.0" encoding="utf-8"?> 和另一个没有.这是问题吗?我该如何解决这个问题?感谢您的帮助,请不要建议使用 rss 解析器库.

When i test this code with the rss news feed from google it works just fine, but if i try it with this other rss feed, it wont work, and gave A LOT* of errors like; Warning: DOMDocument::loadXML() [domdocument.loadxml]: Opening and ending tag mismatch: P line 5 and BODY in Entity, line: 6 in C:\Users\Domingo\Dropbox\www\temp\parser.php on line 18. The two above rss files ARE valid, the only different i found between are that one of those have the line: <?xml version="1.0" encoding="utf-8"?> and the other no. Is this the problem? And how can i solve this? Thanks for any help, and PLEASE do not suggest to use an rss parser libary.

(*) 更多错误如:Warning: DOMDocument::loadXML() [domdocument.loadxml]: Opening and End tag mismatch: BODY line 3 and HTML in Entity, line: 6 in C:\Users\Domingo\Dropbox\www\temp\parser.php 第 18 行Warning: DOMDocument::loadXML() [domdocument.loadxml]: Premature end in tag HTML line 1 in Entity, line: 7 in C:\Users\Domingo\Dropbox\www\temp\parser.php第 18 行

(*) more errors like: 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 and 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

推荐答案

第二个提要本身看起来不错,不包含 and body 或 p 标签.您可能搞混了,加载了错误的文件.

The second feed itself looks fine and does not contain and body or p tags. You're probably mixing things up and are loading the wrong file.

这篇关于使用 PHP 脚本解析 RSS 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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