Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Cross domain PHP Proxy

  1. #1
    Join Date
    Jan 2007
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Cross domain PHP Proxy

    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 Code:
    <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 Code:
    <?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($url04) != '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
    Code:
    // 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.

  2. #2
    Join Date
    Jan 2007
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    12 Views and no reply? Maybe I haven't been clear enough? C'mon guys, I really need some help here.

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    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($url04) != '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)
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  5. #5
    Join Date
    Jan 2007
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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".

  6. #6
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    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.

  7. #7
    Join Date
    Jan 2007
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Jan 2007
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •