What option for cURL needs to be changed in order to retrieve the XML content, as I have added the curlopt
option for xml content but all that happens is the JS error of "no data received"
<?php
$agent = $_SERVER["HTTP_USER_AGENT"];
$timeout = 5; // set to zero for no timeout
if (isset($_GET['url'])) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_GET['url']);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_PROXY, "http://192.168.xx.xx:xxxx");
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents_curl = curl_exec($ch);
curl_close($ch);
//header('Content-Type: text/xml');
echo $file_contents_curl("http://".SanitizeString($_GET['url']));
}
function SanitizeString($var) {
$var = strip_tags($var);
$var = htmlentities($var);
return stripslashes($var);
}
Now the JS part
<html>
<head>
<title>Ajax XML Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Loading XML content into a DIV</h2>
<div id='info'> This sentence will be replaced</div>
<script>
nocache = "&nocache=" + Math.random() * 1000000
//url = "rss.news.yahoo.com/rss/topstories"
url = "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 != null)
{
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
{
var request = new XMLHttpRequest()
}
catch(e1)
{
try
{
request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch(e2)
{
try
{
request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch(e3)
{
request = false
}
}
}
return request
}
</script>
</body>
</html>