0

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. I can access the posted vals using $_POST in that file. That's all about the existing method and it works fine.

Requirement
To hide the customer token value from being seen from client side. So I started with sending server side post request.

Problem
I tried with many options but the callback is not happening -

1) CURL method

$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, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,  $encoded);
$resp = curl_exec($ch);
curl_close($ch);
echo $resp;

$resp echoes 1 if the line curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); is removed but the callback does not happen. I am setting a session variable in the callback script to verify.Is it needed that the API be synchronous in order to use curl method, so that curl_exec returns the response?

2) without CURL as given in Posting parameters to a url using the POST method without using a form

But the callback is not happening.

I tried with the following code too, but looks like my pecl is not installed properly because the HttpRequest() is not defined.

$req = new HttpRequest($apiUrl, HttpRequest::METH_POST);
$req->addQueryData($params);

try 
{
    $r->send();
    if ($r->getResponseCode() == 200) 
    {
     echo "success";
        // success!
    }
    else 
    {
     echo "failure";
        // got to the API, the API returned perhaps a RESTful response code like 404
    }
}
catch (HttpException $ex) 
{
    // couldn't get to the API (probably)
}

Please help me out! I just need to easily send a server side post request and get the response in the callback file.

Community
  • 1
  • 1
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144
  • 1
    I guess the problem is elsewhere. The callback is totally a responsibility of API and as long as method 1 (cURL) returning `1` (which I guess is intended output), the problem is NOT here. – shamittomar Aug 31 '10 at 12:54

2 Answers2

0

Try to debug your request using the curl_get_info() function:

$header = curl_getinfo($ch);
print_r($header);

Your request might be OK but it my result in an error 404.

EDIT: If you want to perform a post request, add this to your code:

curl_setopt($ch, CURLOPT_POST, true);

EDIT: Something else I mentioned at your code: You used a '1' at the 'CURLOPT_RETURNTRANSFER' but is should be 'true':

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

At least this is how I usually do it, and you never know if the function will also understand a '1' as 'true';

EDIT: The real problem: I copy-pasted your source and used it on one of my pages getting this error:

Warning: urlencode() expects parameter 1 to be string, array given in C:\xampp\htdocs\phptests\test.php on line 8

The error is in this line:

foreach($_postArray as $name => $value) 

$_postArray is an array with one value holding the other values and you need either another foreach or you simple use this:

foreach($_postArray['customer_token'] as $name => $value) 
2ndkauboy
  • 9,302
  • 3
  • 31
  • 65
  • the output of `curl_getinfo($ch)` gives - Array ( [url] => http:// [content_type] => text/html [http_code] => 200 [header_size] => 163 [request_size] => 743 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.183987 [namelookup_time] => 0.175189 [connect_time] => 0.175267 [pretransfer_time] => 0.175276 [size_upload] => 586 [size_download] => 0 [speed_download] => 0 [speed_upload] => 3185 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0.183932 [redirect_time] => 0 )` So `http_code` is 200 (looks like no error). – Sandeepan Nath Aug 31 '10 at 14:18
  • Than your request is just empty. But I just saw, that you are trying to make a post request. Than you also have to tell CURL about that. See my edit. – 2ndkauboy Aug 31 '10 at 14:21
  • I did not write `curl_setopt($ch, CURLOPT_POST, true);` in my question by mistake. Now added. Thanks, but the array output which I showed is for the case when `curl_setopt($ch, CURLOPT_POST, true);` is present in code. – Sandeepan Nath Aug 31 '10 at 14:28
  • ok I replaced the 0s and 1s in code and my question but no success. Should the `CURLOPT_RETURNTRANSFER` be true or false? If I keep it true, `echo $resp;` does not give 1. I think that is just for getting the response. – Sandeepan Nath Aug 31 '10 at 14:36
  • It must be true. But I think I found the real issue with your request, see my last edit. – 2ndkauboy Aug 31 '10 at 14:39
  • No that part is correct... In the code of the curl method I wrote above, I have all the posted values in $_postArray (it is actually the $_POST array). Then I am adding only the customer token to the post array and doing the post to API (for hiding the token from client side). So, the foreach is correct. I have echoed the `$encoded` string to verify that. – Sandeepan Nath Aug 31 '10 at 18:41
  • Than the CURL code is also correct and the real issues is in the script behind API_URL. Everything else works. Is that magic API so secure that you can't post us the link so we can test it yourselves to find the issue? – 2ndkauboy Aug 31 '10 at 18:59
  • Please read my question http://stackoverflow.com/questions/3617166/using-curls-curlopt-followlocation-when-open-basedir-is-set-api-callback-faili I got some code which says that I need to redirect to that location along with the post. – Sandeepan Nath Sep 01 '10 at 10:45
0

As discussed in the previous question, the callback is an entirely separate thing from your request. The callback also will not have your session variables, because the remote API is acting as the client to the callback script and has its own session.

You should really show some API documentation here. Maybe we're misunderstanding each other but as far as I can see, what you are trying to do (get the callback value in the initial CURL request) is futile, and doesn't become any less futile by asking twice.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I had left trying to get the callback value in the initial CURL request in my previous question after agreeing to your explanation. Now I am just trying to set a session value in the callback file, but that is not happening. – Sandeepan Nath Aug 31 '10 at 14:21
  • @sandeepan yes, in the callback file, the API acts as the "browser" so it will have a different session than your current one. Usually, the API should return a unique key in the callback so you can tell which request it belongs to (or even continue the session if the key is a session ID). Is there really nothing to that effect in your API? That would be strange. – Pekka Aug 31 '10 at 14:25
  • There are many strange things about this API, due to which I have struggled a lot. It is a still under bug fixing and they make changes when we find bugs. – Sandeepan Nath Aug 31 '10 at 14:30