从服务器端将参数发布到 API URL 后,未发生 API 回调

发布于 2024-09-16 20:31:43 字数 1738 浏览 11 评论 0原文

API集成说明
API 需要将一个表单发布到 API URL,其中包含一些输入字段和客户令牌。 API 处理然后将响应发布到我的服务器上的callback.php 文件。我可以使用该文件中的 $_POST 访问发布的值。这就是现有方法的全部内容,并且效果很好。

要求
隐藏客户令牌值,不让客户端看到。所以我开始发送服务器端发布请求。

问题
我尝试了很多选项,但回调没有发生 -

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

2) 没有 CURL,如 使用 POST 方法而不使用表单将参数发布到 url

但回调没有发生。

我也尝试使用以下代码,但看起来我的 pecl 未正确安装,因为 HttpRequest() 未定义。

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

请帮帮我!我只需要轻松发送服务器端发布请求并在回调文件中获取响应。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉酒的小男人 2024-09-23 20:31:43

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

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

您的请求可能没问题,但我的结果是错误404。

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

curl_setopt($ch, CURLOPT_POST, true);

编辑:我在你的代码中提到的其他内容:你在“CURLOPT_RETURNTRANSFER”处使用了“1”,但应该是“true”:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

至少这是我通常这样做的方式,你永远不知道如果该函数也将“1”理解为“true”;

编辑:真正的问题:我复制粘贴了您的源代码并在我的一个页面上使用它,出现此错误:

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 或者你简单地使用这个:

foreach($_postArray['customer_token'] as $name => $value) 

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) 
塔塔猫 2024-09-23 20:31:43

正如上一个问题中所述,回调与您的请求完全分开。回调也不会包含您的会话变量,因为远程 API 充当回调脚本的客户端,并且拥有自己的会话

您确实应该在这里展示一些 API 文档。也许我们互相误解了,但据我所知,您尝试做的事情(在初始 CURL 请求中获取回调值)是徒劳的,并且通过询问两次也不会变得更徒劳。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文