dowaw80220 2011-08-31 19:18
浏览 262
已采纳

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

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"=>"my@email.com",
                    "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?

  • 写回答

1条回答 默认 最新

  • doushui3216 2011-08-31 19:21
    关注

    You don't need http_build_query because CURLOPT_POSTFIELDS accepts an array

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置