API integration description
The API needs a form to be posted to the API URL with some input fields and a customer token. The API processes and then posts response to a callback.php file on my server (it is fixed as per the documentation and we cannot mention that we want the response to be posted to some other file). I can access the posted vals using $_POST in that file. That's all about the existing method and it works fine.
Now, I want to know if it is possible to get the entire response through curl as a return value of the curl_exec() function?. I am not experienced with curl.
I executed the following code :-
$ch = curl_init(API_URL);
$encoded = '';
$_postArray['customer_token'] = API_CUSTOMER_TOKEN;
foreach($_postArray as $name => $value)
{
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
$resp = curl_exec($ch);
curl_close($ch);
echo $resp;
There is no response but if I comment out the curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
line, the $resp value echoes 1 instead of an array ($_POST) like I expected. Does this 1 mean that succesful handling by API has been detected?
If this is not possible, tell me how to modify the above to at least get the response in the callback.php file as $_POST like it was happening earlier.
More description of Current status and what I wanted
I wanted to get the response as a return value of curl_exec() so that I could use ajax response to redirect the page in frontend to a success page (I wanted to call curl posting through ajax on form submission from frontend keeping the same page in display until response from API comes).
Currently I was doing this by posting the API form through a hidden iframe(to keep displaying the same page), starting a javascript counter to detect a session['api_success'] variable, and setting the session['api_success'] in the callback.php file (after successful response from API). I wanted to discard that method as it can break any time and it fails in linux firefox 2.
Updates
Agreed that with this kind of asynchronous API, there is no other option but to do frequent polling to set a status flag. So, please help me alter the above code so as to receive response in the callback file. Currently it is not coming.
Thanks, Sandeepan