Php XML分析错误:获取RSS源时文档元素后出现垃圾

Php XML分析错误:获取RSS源时文档元素后出现垃圾,php,xml,ajax,rss,Php,Xml,Ajax,Rss,我试图使用Ajax获取RSS提要文档,提取与“titles”标记相关联的值,并将它们回显到屏幕上xmlget.htm通过GET请求实现Ajax。 xmlget.php使用php函数文件_get_内容,以get变量$_get['URL']中提供的URL加载到网页中,并在屏幕上显示“title”标记。 我得到的错误是: XML Parsing Error: junk after document element Location: moz-nullprincipal:{2f186a54-8730-4

我试图使用Ajax获取RSS提要文档,提取与“titles”标记相关联的值,并将它们回显到屏幕上

xmlget.htm通过GET请求实现Ajax。

xmlget.php使用php函数文件_get_内容,以get变量$_get['URL']中提供的URL加载到网页中,并在屏幕上显示“title”标记。

我得到的错误是:

XML Parsing Error: junk after document element Location: moz-nullprincipal:{2f186a54-8730-4ead-9bf9-f82c8d56ad8f} Line Number 2, Column 1:
xmlget.htm

<html>
<head>
    <title>Ajax Example</title>
</head>
<body>
    <h1 style="text-align: center;">Loading a web page into a DIV</h1>
    <div id='info'>This sentence will be replaced</div>
<script>
    nocache = "&nocache="+Math.random()*1000000
    url = "rss.news.yahoo.com/rss/topstories"
    request = new ajaxRequest()
    request.open("GET","xmlget.php?url="+url+nocache,true)
    out = "";

    request.onreadystatechange = function(){
        if(this.readyState == 4){
            if(this.status == 200){
                if(this.responseXML != ""){
                    titles = this.responseXML.getElementsByTagName('title')
                    for (j = 0 ; j < titles.length ; ++j)
                        out += titles[j].childNodes[0].nodeValue + '<br />'
                    document.getElementById('info').innerHTML = out
                }
                else alert("Ajax error: No data received")
            }
            else alert( "Ajax error: " + this.statusText)
        }    
    }

    request.send(null)

    function ajaxRequest(){
        try{
            request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e1){
            try{
                request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e2){
                try{
                    request = new XMLHttpRequest()
                } catch (e3){
                    request = false
                }
            }
        }
        return request
    }
</script>
</body>

Ajax示例
将网页加载到DIV中
这句话将被替换
nocache=“&nocache=“+Math.random()*1000000
url=“rss.news.yahoo.com/rss/topstories”
请求=新的ajaxRequest()
open(“GET”、“xmlget.php?url=“+url+nocache,true”)
out=“”;
request.onreadystatechange=函数(){
if(this.readyState==4){
如果(this.status==200){
如果(this.responseXML!=“”){
titles=this.responseXML.getElementsByTagName('title'))
对于(j=0;j'
document.getElementById('info').innerHTML=out
}
else警报(“Ajax错误:未收到数据”)
}
else警报(“Ajax错误:+this.statusText)
}    
}
请求发送(空)
函数ajaxRequest(){
试一试{
请求=新的ActiveXObject(“Microsoft.XMLHTTP”);
}渔获物(e1){
试一试{
请求=新的ActiveXObject(“Msxml2.XMLHTTP”);
}渔获物(e2){
试一试{
请求=新的XMLHttpRequest()
}渔获物(e3){
请求=false
}
}
}
退货申请
}

xmlget.php

<?php
if(isset($_GET['url'])){

    function SanitizeString($var) {
        $var = strip_tags($var);
        $var = htmlentities($var);
        return stripcslashes($var);
    }

    header('Content-Type: text/xml');
    echo file_get_contents("http://www.".SanitizeString($_GET['url']));

}

请在标题中添加以下行

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />

找到了问题! file\u get\u contents函数无法找到主机,因为url无效。rofl…

不正确

echo file_get_contents("http://www.".SanitizeString($_GET['url']));
echo file_get_contents("http://".SanitizeString($_GET['url']));
正确

echo file_get_contents("http://www.".SanitizeString($_GET['url']));
echo file_get_contents("http://".SanitizeString($_GET['url']));

对不起,这没用