pazof
pazof

Reputation: 964

execute CURL request via AJAX-called PHP script

I'm struggling to understand why a known-to-be-working CURL request does not work when called via an AJAX request.

Suppose we have a curl.php file, which executes some CURL request. These are its contents:

<?php
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "https://www.somewebsite.com/somescript.cgi");         // set the URL
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); // return the result instead of outputting it
$output = curl_exec($c);
curl_close($c);
?>

and we call it via JQuery like so:

$.ajax({
    url: 'curl.php',
    data: {
        someVariable: "someValue"
    },
    dataType: "json",
    success: function(response){
        //do stuff
    }
});

The CURL request is supposed to trigger a certain action on the remote server (in this case it calls a cgi script, which inserts a record in some remote database).

UPDATE: In order for the remote cgi script to be executed, a cookie has to be set to my browser from the remote website. (which is indeed the case - the said cookie is set)

If I run the curl.php script alone, the CURL request in it works perfectly (the remote action is triggered). However, if I call it asynchronously via AJAX, although the output I'm getting from it is exactly the same as the one I'm getting if executing the curl.php script alone, the remote action is not triggered - which is what I am after.

I can only come up with 2 possible causes: that the request is maybe being regarded as a cross-domain request and as such is being denied - not sure if this is possible - or that the AJAX request does not wait for the CURL request to be completed. If this is the case, I am not sure if there is a way to "force" it to wait as much as needed.

UPDATE: have come up with a 3rd possible cause - could it be that said cookie is somehow not visible by the remote website if the curl request is executed through an AJAX request? (this would explain the fact that although the curl request itself seems to also be executed correctly when called via AJAX, the desired remote action is yet not triggered)

Would be grateful for any ideas.

Upvotes: 1

Views: 5337

Answers (2)

ChrisBuck
ChrisBuck

Reputation: 102

With respect to forcing the ajax call to wait, I've had success wrapping the ajax operation in a promise. This should make it easy to detect if the curl operation completed successfully, because if the promise doesn't resolve, the "do stuff" operation will never execute. There's also the added benefit of logging the response each step of the way, so you can see what's happening with the curl request.

You can also chain additional then clauses onto the promise, if, say, you need to parse the response before "do stuff." And including a catch for errors should help provide insight into how everything is working.

JavaScript:

var promise = new Promise(function(resolve, reject){
    $.ajax({
        url: 'curl.php',
        data: {
            someVariable: "someValue"
        },
        dataType: "json",
        success: function(response){
            //resolve response, instead of do stuff
            console.log(response);
            resolve(response);
        }
    });
});

promise.then(function(response){
    //do stuff
    console.log(response);
    return(response);
});

Upvotes: 0

Niklesh Raut
Niklesh Raut

Reputation: 34914

Print your output

  echo $output = curl_exec($c);

Or

   $output = curl_exec($c);
   echo json_encode($output);// if array value

Upvotes: 1

Related Questions