我正在使用Webex的URL API,出于某种原因,在向URL编写简单的PHP cURL请求时,API会返回失败。但是,如果我将相同的post参数传递到表单中,并且表单的action属性等于该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"=>"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;
}
问题:为什么我卷曲时API会失败?为什么在将它用作表单后置操作时会成功? cURL方法有什么问题?
答案 0 :(得分:1)
您不需要http_build_query
因为CURLOPT_POSTFIELDS接受数组