trippin
02-11-2007, 06:14 PM
Hey, I'm extremely new with php and resorted to this forum after learning that my intentions of cross-domain data retrieval were not possible using Javascript.
I have been searching for hours and not having any luck thus far even though I found a few rough script out there.
This is what I have so far (not working for me).
This consists of three files:
File: demo.html
<html><head>
<title>Cross Domain Test</title>
<script type="text/javascript" src="engine.js"></script>
</head><body>
<div id="contentdiv"></div>
<input type="button" onClick="ajax_get('getfile.php', 'ygmauser');" value="Get content" />
</body></html>
File: getfile.php
<?php
// Get URL and div
if (!isset($_GET['url'])) { die(); } else { $url = $_GET['url']; }
if (!isset($_GET['el'])) { die(); } else { $el = $_GET['el']; }
// Make sure url starts with http
if (substr($url, 0, 4) != 'http') {
// Set error
echo 'alert(\'Security error; incorrect URL!\');';
die();
}
// Try and get contents
$data = @file_get_contents($url);
if ($data === false) {
// Set error
echo 'alert(\'Unable to retrieve "' . $url . '"\');';
die();
}
// 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);
?>
el = document.getElementById('<?php echo $el; ?>');
el.innerHTML = '<?php echo $data; ?>';
File: engine.js
// Get base url
url = "http://companion.yahoo.com";
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);
function ajax_get (url, el) {
// Has element been passed as object or id-string?
if (typeof(el) == 'string') {
el = document.getElementById(el);
}
// Valid el?
if (el == null) { return false; }
// Does URL begin with http?
if (url.substring(0, 4) != 'http') {
url = base_url + url;
}
// Create getfile URL
getfile_url = base_url + 'getfile.php?url=' + escape(url) + '&el=' + escape(el.id);
// Do Ajax
ajax_do (getfile_url);
return true;
}
function ajax_do (url) {
// Does URL begin with http?
if (url.substring(0, 4) != 'http') {
url = base_url + url;
}
// Create new JS element
var jsel = document.createElement('SCRIPT');
jsel.type = 'text/javascript';
jsel.src = url;
// Append JS element (therefore executing the 'AJAX' call)
document.body.appendChild (jsel);
}
I am wanting to get the html of the DIV value "ygmauser" on "http://companion.yahoo.com". This is for testing purposes.
I have been searching for hours and not having any luck thus far even though I found a few rough script out there.
This is what I have so far (not working for me).
This consists of three files:
File: demo.html
<html><head>
<title>Cross Domain Test</title>
<script type="text/javascript" src="engine.js"></script>
</head><body>
<div id="contentdiv"></div>
<input type="button" onClick="ajax_get('getfile.php', 'ygmauser');" value="Get content" />
</body></html>
File: getfile.php
<?php
// Get URL and div
if (!isset($_GET['url'])) { die(); } else { $url = $_GET['url']; }
if (!isset($_GET['el'])) { die(); } else { $el = $_GET['el']; }
// Make sure url starts with http
if (substr($url, 0, 4) != 'http') {
// Set error
echo 'alert(\'Security error; incorrect URL!\');';
die();
}
// Try and get contents
$data = @file_get_contents($url);
if ($data === false) {
// Set error
echo 'alert(\'Unable to retrieve "' . $url . '"\');';
die();
}
// 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);
?>
el = document.getElementById('<?php echo $el; ?>');
el.innerHTML = '<?php echo $data; ?>';
File: engine.js
// Get base url
url = "http://companion.yahoo.com";
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);
function ajax_get (url, el) {
// Has element been passed as object or id-string?
if (typeof(el) == 'string') {
el = document.getElementById(el);
}
// Valid el?
if (el == null) { return false; }
// Does URL begin with http?
if (url.substring(0, 4) != 'http') {
url = base_url + url;
}
// Create getfile URL
getfile_url = base_url + 'getfile.php?url=' + escape(url) + '&el=' + escape(el.id);
// Do Ajax
ajax_do (getfile_url);
return true;
}
function ajax_do (url) {
// Does URL begin with http?
if (url.substring(0, 4) != 'http') {
url = base_url + url;
}
// Create new JS element
var jsel = document.createElement('SCRIPT');
jsel.type = 'text/javascript';
jsel.src = url;
// Append JS element (therefore executing the 'AJAX' call)
document.body.appendChild (jsel);
}
I am wanting to get the html of the DIV value "ygmauser" on "http://companion.yahoo.com". This is for testing purposes.