1

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.

7
  • Do you have any errors? Commented Feb 15, 2017 at 17:33
  • Your PHP script doesn't appear to do anything with input variables (The data section of your AJAX request populates $_GET or $_POST depending on the request method) and it doesn't seem to actually output anything (You assign whatever comes out of the curl request to $output but then you don't actually do anything with $output). Commented Feb 15, 2017 at 17:34
  • @Ostin Nope, nothing. curl_error() is empty. Weird thing is, the CURL request's output that I'm getting when I execute it via AJAX is the same (correct) as the one that I'm getting when executing the PHP script alone. Problem is, the CURL request is meant to trigger a certain function to the remote server, which, in the AJAX case, is not triggered. (updating the question accordingly) Commented Feb 15, 2017 at 17:36
  • You've got a good answer below, but FYI you could make this script a lot easier: <?php echo file_get_contents("https://www.somewebsite.com");?> Commented Feb 15, 2017 at 17:40
  • also tried with file_get_contents, didn't work either... Commented Feb 15, 2017 at 17:42

2 Answers 2

1

Print your output

  echo $output = curl_exec($c);

Or

   $output = curl_exec($c);
   echo json_encode($output);// if array value
Sign up to request clarification or add additional context in comments.

8 Comments

I am getting the same (correct) output from CURL in both cases. (Apologies - please see the updated question)
If your output is not json value remove this from ajax dataType: "json",
I do require it to be json - tried removing it for the sake of testing, still didn't work though
what output echo $output = curl_exec($c); you are getting ?
I'm getting an image, which is indeed what I should be getting, judging by the output when it works as expected.
|
0

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);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.