从API回调从服务器端发布参数API URL后没有发生 [英] Callback from API not happening after posting parameters to API URL from server side

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

问题描述

API集成说明结果
该API需要被张贴到一些输入字段和客户令牌API URL的形式。该API过程再上岗回应我的服务器上callback.php文件。我可以访问该文件使用$ _ POST张贴丘壑。这是所有关于现有的方法,它工作正常。

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呼应1,如果行 curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真实); 被删除,但回调不会发生。我在回调脚本中的会话变量设置为它需要verify.Is该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),而卷曲在<给定href=\"http://stackoverflow.com/questions/1244308/posting-parameters-to-a-url-using-the-post-method-without-using-a-form\">http://stackoverflow.com/questions/1244308/posting-parameters-to-a-url-using-the-post-method-without-using-a-form

但回调没有发生。

我试着用下面的code太多,但貌似没有安装我的PECL正确,因为Htt的prequest()没有定义。

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

请帮助我!我只需要轻松地发送一个服务器端的POST请求,并得到在回调文件的响应。

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

推荐答案

尝试使用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.

修改:如果你要执行post请求,将它添加到您的code:

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

curl_setopt($ch, CURLOPT_POST, true);

修改:别的东西,我在你的code提到:你在CURLOPT_RETURNTRANSFER'用'1',但应该是'真':

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为真;

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是一个值保持另一个值的数组,你需要或者其他的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 URL后没有发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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