DISQUS

Md Emran Hasan (phpfour): Cross-Domain AJAX calls using PHP

  • Loïc Hoguin · 1 year ago
    Please please please add some caching mechanism to your script. You do not want to allow some kiddie to flood another website using your script. Even 1-minute cache is enough, but just don't call this website everytime like this. Not only will it be slow but the other site owner might get angry at you.
  • H2 · 1 year ago
    Does it also support POST simulation?

    Good Work Batman!
  • Md Emran Hasan (phpfour) · 1 year ago
    @H2: Of course. Thanks !
  • Saidur Rahman · 1 year ago
    This is a nice tutorial with explanation. Well I want t o show if you want to use jquery (http://jquery.com/).

    If you want to get json data , then you can easily use getJSON.

    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
    function(data){
    $.each(data.items, function(i,item){
    $("").attr("src", item.media.m).appendTo("#images");
    });
    });


    Or if you don’t want to get other type of data like XML or other type, then you have to use cURL in PHP:

    $.ajax({
    type: "GET",
    url : ”http://myserver.com/transport.php?action=’ +
    urlencode(‘different-server.com/return_call.php’) +
    ‘&method=get&data1=101&data2=pass”,
    success: function(serverResponse)
    /*parse serverResponse data */
    });
  • zack · 1 year ago
    I think that Loic Hoguin's recommendation is accurate.
    No comment ?
  • Md Emran Hasan (phpfour) · 1 year ago
    @Loic Hoguin: I completely agree with you that this script shouldn't allow other person from web to misuse it for their own use.

    But I would suggest using $_SERVER['REMOTE_ADDR'] to block request from scripts other than the server I'm invoking it from.

    The reason I think that way is, in the project I used it, the other server was also mine and I needed to communicate between them. As the output was quite dynamic, caching couldn't be done.

    But then again, if needed, caching can be added easily. I will be uploading one with caching later tonight.

    Thanks everybody for commenting !
  • Loïc Hoguin · 1 year ago
    Indeed, filtering using REMOTE_ADDR is the best if both servers are yours.

    What's important to note, too, is that if curl_exec can't fetch the data for some reasons it will return false. That means that your script will echo nothing. People using it should take care on the javascript side to check the data they get and if it's empty display an error.
  • Md Emran Hasan (phpfour) · 1 year ago
    @Loic Hoguin: Yes, you're right. Also, to be safe, using a http wraper class supporting both cURL and fsockopen (like mine one) can be suggested to avoid problem.
  • SuNcO · 1 year ago
    On the transport file you can't get the action because is named url

    You must change that line :

    $action = $_REQUEST['url'];

    To :

    $action = $_REQUEST['action'];

    Or change this :

    xmlHttp.open(“GET”, ‘http://myserver.com/transport.php?action=’ + 10: urlencode(‘different-server.com/return_call.php’) + 11: ‘&method=get&data1=101&data2=pass’, true );

    With this :

    xmlHttp.open(“GET”, ‘http://myserver.com/transport.php?url=’ + 10: urlencode(‘different-server.com/return_call.php’) + 11: ‘&method=get&data1=101&data2=pass’, true );
  • François Labarde · 1 year ago
    there's a typo:

    you wrote this:
    if ($key != 'url' || $key != 'method')

    while you obviously meant this:
    if ($key != 'url' && $key != 'method')


    (since ($key != 'url' || $key != 'method') is always true)
  • Kelvin · 1 year ago
    I have tried this proxy approach, but neither Firefox nor IE 6 will allow me to call a php page with XMLHttpRequest. Of course, it won't allow me to call webservice cross-domain either.
    Thanks.
  • Rene Veerman · 1 year ago
    Hi, i've tried using your script and on my homeserver it runs fine but on my shared hoster it hangs at curl_exec().
    Is there anything you know of that causes this freezing/hanging?
  • rollstag · 11 months ago
    Nice to know that.
    Thank you
  • Nick Syed · 11 months ago
    I am using the following code, I get an error saying Object Not Found, can you please help ?

    <html>
    <body>

    <script type="text/javascript">

    function ajaxFunction()

    {

    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
    {
    document.myForm.time.value=xmlHttp.responseText;
    }
    }
    xmlHttp.open("GET","transport.php?action="+urlencode("http://www.netshelter.net/time.php"),true);
    xmlHttp.send(null);

    }

    </script>

    <form name="myForm">
    Name: <input type="text" onkeyup="ajaxFunction();" name="username" />
    Time: <input type="text" name="time" />
    </form>

    </body>
    </html>
  • Daniel · 9 months ago
    Hi, thanx a lot for the code!!

    I've made the following solution with Jquery + PHP:

    $.get("http://mydomain.com/transport.php?action=<? echo urlencode('http://www.anotherdomain.com'); ?>&method=get&var1=variable1&var2=variable2&var3=variable3",
    function(serverResponse) {
    alert(serverResponse);
    }
    );

    And I had to change the transport.php on the line 18 to: $action = $_REQUEST['action'];

    It worked very well!!

    Thx again
  • Piro · 8 months ago
    My target page has '?page=x' to get the different contents according to x.
    In this case, this script ignores '?page=x' and it results in getting the default contents.

    Could you please give me some hints how to fix this problem.

    Thank you!
  • Sumeet · 5 months ago
    it gives error urlencode not found?
  • Wish all · 5 months ago
    Hi,

    I am new to PHP. I couldnt find urlencode function in javascript. So to make it workable in my code, I have changed the urlencode to escape function.

    ie
    xmlHttp.open("GET", 'http://myserver.com/transport.php?action=' + 10: escape('different-server.com/return_call.php') + 11: '&method=get&data1=101&data2=pass', true );

    Is it correct?

    Regards,