AJAX和PHP问题!
|
我有一个PHP页面,该页面将从downloads.nl收集mp3链接。结果转换为XML并呈现良好。
当我尝试使用Ajax读取XML时,会出现问题。这些文件位于同一域中,这真让我感到困惑。这是我的PHP搜寻器。
<?php
header(\"Content-type: text/xml\");
$artistname = $_GET[\'artistname\'];
$trackname = $_GET[\'trackname\'];
$newartistname = str_replace(\" \",\"+\",$artistname);
$newtrackname = str_replace(\" \",\"+\",$trackname);
$target_url = \"http://www.downloads.nl/results/mp3/1/\".$newartistname.\"+\".$newtrackname;
$userAgent = \'Googlebot/2.1 (http://www.googlebot.com/bot.html)\';
error_reporting(0);
// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);
if (!$html) {
echo \"<br />cURL error number:\" .curl_errno($ch);
echo \"<br />cURL error:\" . curl_error($ch);
exit;
}
// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate(\"/html/body//a\");
echo \'<?xml version=\"1.0\"?>\';
echo \'<downloads>\';
echo \'<trackname>\'.$newartistname.\"+\".$newtrackname.\'</trackname>\';
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute(\'href\');
if(strpos($url, \".cgi\")){
echo \'<link>http://downloads.nl\'.htmlspecialchars($url,ENT_QUOTES).\'</link>\';
}
}
echo \'</downloads>\';
?>
这是我的JavaScript函数
function getDownloadLink(artistname,trackname){
var xmlhttp4;
if (window.XMLHttpRequest){
xmlhttp4 = new XMLHttpRequest();
}
else{
xmlhttp4 = new ActiveXObject(\"Microsoft.XMLHTTP\");
}
xmlhttp4.onreadystatechange=function(){
alert(xmlhttp4.readyState);
if (xmlhttp4.readyState==4 && xmlhttp4.status==200){
try{
var downloadlink = xmlhttp4.responseXML.documentElement.getElementsByTagName(\"downloads\");
for (var i=0;i<downloadlink.length;i++){
alert(i);
}
}
catch(er){
alert(xmlhttp4.responseText);
}
}
else{
alert(\"ReadyState: \"+xmlhttp4.readyState+\" Status: \"+xmlhttp4.status);
}
}
xmlhttp4.open(\"GET\",\"http://localhost/bone/searchmusic.php?artistname=\"+artistname+\"&trackname=\"+trackname,true);
xmlhttp4.send(null);
}
我不知道是什么问题。我无法正确呈现XML还是缺少ajax?
谢谢,
山姆
没有找到相关结果
已邀请:
2 个回复
屉杆绊
…假设您有一个名为downloads.xml的文件,格式如下:
该代码实际上并没有使用演出者和曲目,但我添加了它们以使您了解其工作方式。
梦话快家腹
再次感谢。