Just a few clean-ups and modifications:
demo.html
HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function makeRequest(url, elementId, getpost, senddata) {
if(getpost == "undefined" || getpost != "POST" || getpost != "GET") {
getpost = "GET";
}
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
// See note below about this line
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
throw('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = function() {
if (http_request.readyState == 4 && http_request.status == 200) {
document.getElementById(elementId).innerHTML = http_request.responseText;
}
else {
document.getElementById(elementId).innerHTML = "Please wait..."
}
};
http_request.open(getpost, url, true);
if(getpost == "POST") {
http_request.send(senddata);
} else {
http_request.send(null);
}
}
function getUrl(url,elementId) {
makeRequest('getfile.php?url=' + escape(url) + '&el=' + escape(elementId),elementId)
}
</script>
</head>
<body>
<div id="contentdiv"></div>
<form name="form">
<input type="button" onClick="getUrl('http://www.google.com.au/', 'contentdiv');" name="get" id="get" value="Get content" />
</form>
</body>
</html>
getfile.php
PHP Code:
<?php
// Get URL and div
if (!isset($_GET['url'])) {
echo "URL not specified.";
}
else {
$url = urldecode($_GET['url']);
}
// Make sure url starts with http
if (substr($url, 0, 4) != 'http') {
// Set error
echo 'Invalid URL - '.$url;
return false;
}
// Try and get contents
$data = @file_get_contents($url);
if ($data === false) {
// Set error
echo 'Unable to retrieve $url';
return false;
}
// Escape data
/*
$data = str_replace("'", "\'", $data);
$data = str_replace('"', "'+String.fromCharCode(34)+'", $data);
$data = str_replace ("\r\n", '\n', $data);
$data = str_replace ("\r", '\n', $data);
$data = str_replace ("\n", '\n', $data);*/
echo $data;
?>
I've tested it on Google and it does work (without the images)
Bookmarks