views:

66

answers:

2

Is there a way to get data generated by AJAX using php Curl? Thank you.

A: 

Yes you can use JQuery (which you can get here):

<script>
var lat = $.ajax({
url: "function.php?function=curl",
async: false
}).responseText;
</script>

And then inside function.php you do your CURL, echo what you want, and you'll have the response in your lat JS Var. So if you do this:

<script> alert (lat); </script>

You should see the data from the CURL.

In function.php you'll need your curl to look like this:

if($_GET["function"] == "curl") {
$url = 'http://whichever.url.you.need';
 $curl_handle = curl_init();
  curl_setopt($curl_handle, CURLOPT_URL, $url);
  curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
  curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl_handle, CURLOPT_COOKIEJAR, $cookie_file_path);
  curl_setopt($curl_handle, CURLOPT_COOKIEFILE, $cookie_file_path);
  //curl_setopt($curl_handle, CURLOPT_POST, 1);
  //curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post);
  $buffer = curl_exec($curl_handle);
  curl_close($curl_handle);
  // check for success or failure
  if (empty($buffer)) {
  echo 'Something went wrong :(';
  } else {
  echo $buffer;
  }
    }

This blog tells you a little more information on this: AJAX Transfer

Pete Herbert Penito
I didn't get it. Thank you.
thomas
Your welcome, I have edited in a little more into the answer.
Pete Herbert Penito
It sounds like he wants use cUrl to get the generated AJAX data.
stjowa
Yes, it is stjowa...
thomas
+2  A: 

If you know the AJAX call URL, you can pretty much treat it like any other URL and get it with CURL. You can find out the AJAX request URL by using firebug or similar tools, make sure you understand all the parameters being sent including POST.

However the data might be in JSON format, which is then displayed with Javascript in some tabular or other format, you will have to figure that part out yourself.

Sabeen Malik
thomas
Sabeen Malik