Curl返回成功,但API调用失败

发布于 2024-12-02 20:53:27 字数 1948 浏览 0 评论 0原文

我正在使用 Webex 的 URL API,由于某种原因,当向 URL 写入简单的 PHP cURL 请求时,API 返回失败。但是,如果我将相同的 post 参数传递到表单中,并且表单的操作属性等于该 API 端点,则 API 将返回成功。

这是表单方法:

  <form action="xxxxxxx987" name="hidden_form" method="post">
      <input value="EN" name="AT" type="hidden" />
      <input value="xxxxxxx987" name="MK" type="hidden" />
      <input value="<?php echo $email; ?>" name="AE" type="hidden" />
      <input value="<?php echo $firstname; ?>" name="FN" type="hidden" />
      <input value="<?php echo $lastname; ?>" name="LN" type="hidden" />
      <input value="<?php echo $company; ?>" name="CO" type="hidden" />
      <input value="http://mysite.com/resources/thank_you" name="BU" type="hidden" />
  </form>

这是 cURL 方法:

$url = "https://mysite.com/m.php";
//Data Array
$postParams = array("AT"=>"EN",
                    "MK"=>"xxxxxxx987",
                    "AE"=>"[email protected]",
                    "FN"=>"fname",
                    "LN"=>"lname",
                    "CO"=>"my company",
                    "BU"=>"http://192.168.x.x/resources/thank_you");
//Encode Query Data
$data = http_build_query($postParams);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); //True For Regular HTTP Post
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded", "Content-length: ".strlen($data)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);

if($result) {
    echo '<h3>Status: Curl Succeeded</h3>';
    print 'Result: '.$result;
}

问题:为什么当我卷曲 API 时 API 会失败,而当使用它作为表单发布操作时为什么会成功? cURL 方法有什么问题?

I'm using Webex's URL API and for some reason, when writing a simple PHP cURL request to the URL, the API returns failure. But if I pass in the same post parameters into a form, and the form's action attribute equals that API endpoint, the API returns success.

Here's the form method:

  <form action="xxxxxxx987" name="hidden_form" method="post">
      <input value="EN" name="AT" type="hidden" />
      <input value="xxxxxxx987" name="MK" type="hidden" />
      <input value="<?php echo $email; ?>" name="AE" type="hidden" />
      <input value="<?php echo $firstname; ?>" name="FN" type="hidden" />
      <input value="<?php echo $lastname; ?>" name="LN" type="hidden" />
      <input value="<?php echo $company; ?>" name="CO" type="hidden" />
      <input value="http://mysite.com/resources/thank_you" name="BU" type="hidden" />
  </form>

And here's the cURL method:

$url = "https://mysite.com/m.php";
//Data Array
$postParams = array("AT"=>"EN",
                    "MK"=>"xxxxxxx987",
                    "AE"=>"[email protected]",
                    "FN"=>"fname",
                    "LN"=>"lname",
                    "CO"=>"my company",
                    "BU"=>"http://192.168.x.x/resources/thank_you");
//Encode Query Data
$data = http_build_query($postParams);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); //True For Regular HTTP Post
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded", "Content-length: ".strlen($data)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);

if($result) {
    echo '<h3>Status: Curl Succeeded</h3>';
    print 'Result: '.$result;
}

The question: Why does the API fail when I curl it and why does it succeed when using it as the form post action? What's wrong with the cURL method?

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

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

发布评论

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

评论(1

奢华的一滴泪 2024-12-09 20:53:27

您不需要 http_build_query 因为 CURLOPT_POSTFIELDS 接受数组

You don't need http_build_query because CURLOPT_POSTFIELDS accepts an array

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