views:

26

answers:

1

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

A: 

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 don't know the API you're talking about, but most probably no. The API will likely start a background process that triggers the callback when it's done. The original request does not receive a return value in this kind of setup.

Pekka
@Pekka, I edited my question - There is no response initially, 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?
sandeepan
If it is not possible for the original request to receive a return value, then what other alternatives do I have? I do not want to use any kind of for loop to check status after every few milliseconds or so. Any kind of built in waiting logic possible so as to return true response to the ajax call? I want this logic to be in the server side and invisible to the client side. Earlier I had used a javascript logic as described in my question.Thanks
sandeepan
@sandeepan See http://www.php.net/curl_exec `Returns TRUE on success or FALSE on failure.` it only means that the CURL request was made successfully. It does not signify an API response.
Pekka
@sandeepan if the API is asynchronous, you will not have any choice but have the callback script to change some flag on success, and do frequent polling - either on the PHP side, or on client side using Ajax - to detect the flag change!
Pekka
Thanks for your explanations. Agreed. Please read my updates.
sandeepan
@sandeep well, you'll have to build a callback script that alters a flag (e.g. creates a temporary file of a certain name - your API will have to accept a unique value for that, that it passes to the callback), and use a loop in your file above to check for the status of that loop - preferably checking the current time so you can specify a timeout. Pretty straightforward really.
Pekka
@Pekka, actually I am not getting the response in my callback file (probably the API is not posting response). If response comes, then I can code the frequent polling part in server side. I have already done it in client side which was more difficult. But why is the response not coming. I tried setting a session var in the callback file but it did not set.
sandeepan
@sandeepan you'll have to refer to your API docs for that. The API needs to pass a unique key to the callback that you specify in the request. Otherwise, you'll have no chance of finding out which callback is related to which request.
Pekka
@Pekka, the API does not pass a unique key in my case, if it processes correctly, it posts back the response. Now, my question is that if normal form posting works fine, can using curl posting affect the process so that no response comes? I dont think so. May be some editing of the above code is needed.
sandeepan