Log in

View Full Version : Cross domain PHP Proxy



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.

trippin
02-11-2007, 07:27 PM
12 Views and no reply? Maybe I haven't been clear enough? C'mon guys, I really need some help here.

djr33
02-12-2007, 12:03 AM
With no test page and a fairly brief look at the code, I think you're going about this in the wrong way.
I don't think that using javascript on the ajax page would work like that.
You *might* be able to find a way to do something similar using include() so you can access the data as part of the page, but it wouldn't work like you've got it setup.
Javascript isn't cross page compatible, so using an ajax request to get something that is altered by javascript would just get the original thing, ignoring the js (though include it in the page).

The idea here would be to use substr() or explode() commands to split the data of the page to find the div element you need. Find the location of the name="yourname", then split and just keep everything after that. Then split again at the next > sign, meaning the end of that div open tag. Then it's a bit complex, but you would wnat to split at the close tag. The only catch is that there might be several closetags within it, so you should count how many instances of "<div" exist then correct based on that. You could try a trial and error or repeated operation to get it right. I think this makes the most sense.

tech_support
02-13-2007, 06:34 AM
Just a few clean-ups and modifications:

demo.html


<!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
// 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)

trippin
02-13-2007, 09:04 PM
Thanks tech-support for you help! I've modified the files according to your instructions and uploaded them to
http://insta-yah-games.110mb.com/index.html

Its not working for me yet for some reason.
When I click the button I get the text "Unable to retrieve $url" displayed in the "contentdiv".

blm126
02-13-2007, 09:50 PM
trippin: You need to modify your PHP settings. I believe it is allow_url_fopen. Turn it on, and everything should work.

techsupport: Remember, PHP will only check strings that are double quoted for variables.

trippin
02-13-2007, 11:24 PM
How do I modify these settings? I don't understand what you mean by "turning it on". I have it hosted at http://insta-yah-games.110mb.com/index.html. I really appreciate any help.

djr33
02-14-2007, 01:17 AM
The system settings for PHP do not allow for remote files to be accessed (files not on your server).
To allow that, allow_url_fopen must be turned on, and you can set that in the php.ini file on your server root, if you have access to that.

trippin
02-14-2007, 01:32 AM
I don't know if i have access. It is a freehosting site that I am using... Is there a free hosting site that will give me access to the php.ini file?

djr33
02-14-2007, 01:35 AM
Might be time to upgrade.

Run the script <?php php_info(); ?> (as a whole page) to see exactly what is and what is not enabled.
If that appears to be disabled, not sure what you can do.

Look at your cpanel and such to see if there's another way to access it, or contact the host.

For another free host... just use google. Free hosts always have some catch, so you'll just need to find one that allows what you're needing.

trippin
02-14-2007, 06:36 AM
You're right. I am just learning that all the free webhosts do not allow the fopen() or fsocketopen() functions. This really sucks and I wish that I was told this a long time ago lol. Alas, I am not a quitter and I remember reading that curl could be used instead of fopen. Curl is enabled by default on most hosts. How would I go about doing this with curl?

djr33
02-14-2007, 08:51 AM
Not familiar with that function, myself.
But php.net has documentation on every function, so that should help. Just replace with the same parameters and do the rest of the script in the same way. It should be fairly easy.

trippin
02-14-2007, 09:55 AM
Great News! I have a working script using curl. Now any site can be loaded with this script. I have to admit.. I did expect more support on this subject than I have gotten.
Here is my test link http://insta-yah-games.110mb.com/lite.php

My next step is to figure out how to extract certain html and display alone without the rest of the webpage (i.e. table cell, div, ect.)