从服务器端将参数发布到API网址后,API不会发生回调 [英] Callback from API not happening after posting parameters to API URL from server side

查看:442
本文介绍了从服务器端将参数发布到API网址后,API不会发生回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

API集成描述

API需要一个表单发布到API网址,其中包含一些输入字段和一个客户令牌。 API处理,然后发布对我的服务器上的callback.php文件的响应。我可以使用$ _POST在该文件中访问已发布的vals。

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.

需求

隐藏客户令牌值,使其不被看到从客户端。所以我开始发送服务器端的post请求。

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方法

$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如果 curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true ); 被删除,但回调不会发生。我在回调脚本中设置一个会话变量来验证。为了使用curl方法,它需要同步API,以便curl_exec返回响应?

$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)没有CURL,请参阅使用POST方法而不使用表单将参数发布到网址

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

但是回调没有发生。

我试着用下面的代码,但看起来像我的pecl没有正确安装,因为HttpRequest()没有定义。

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)
}

请帮助我!

推荐答案

尝试调试您的请求使用curl_get_info()函数:

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

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

您的请求可能正确,但我的结果为错误404。

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

EDIT :如果您要执行发布请求,请将其添加到您的代码中:

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

curl_setopt($ch, CURLOPT_POST, true);

EDIT :我在代码中提到的一些东西: 1'在'CURLOPT_RETURNTRANSFER',但应该是'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);

至少这是我通常做的,你永远不知道函数是否也会理解'1'as'true';

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

EDIT :真正的问题:我复制粘贴了您的源代码,获取此错误:

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

错误在此行:

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

$ _ postArray是一个数组,其中一个值包含其他值,你需要另一个foreach或者你简单使用这个:

$_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) 

这篇关于从服务器端将参数发布到API网址后,API不会发生回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆