Callback from API not happening after posting para

2019-08-25 09:28发布

问题:

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.

回答1:

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) 


回答2:

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.