【发布时间】:2018-03-20 14:34:15
【问题描述】:
我希望将 xml 文件发布到在 sbs 服务器上运行的 Web 服务,但它需要 2 个后域。一个“用户名”和“秘密”。我将它设置在一个数组中,但由于它的失败,我遗漏了一些东西。我已经在 url 字符串中尝试了 clientid 和 secret,但开发人员已将其作为 post 字段请求。这是我已经拥有的。
add_action('gform_after_submission_73', 'post_to_third_party', 10, 2);
function post_to_third_party($entry)
{
$url = 'http://mywebsitesite.co.uk/webdata.aspx';
$id = array('clientid' => '15', 'secret' => '123');
$encoding = 'WINDOWS-1252';
$brand = htmlspecialchars($entry['20'], ENT_XML1, $encoding);
$product = htmlspecialchars($entry['22'], ENT_XML1, $encoding);
$form_id = htmlspecialchars($entry['21'], ENT_XML1, $encoding);
$xml = "<?xml version=\"1.0\" encoding=\"$encoding\"?>
<webform>
<brand>$brand</brand>
<product>$product</product>
<form_id>$form_id</form_id>
</webform>";
$ch = curl_init($url);
if ($ch === false) {
throw new RuntimeException("Unable to initialise a session");
}
$result = curl_setopt_array($ch, [
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => ['Content-Type: text/xml'],
CURLOPT_POSTFIELDS => $xml,
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $id,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => 1,
]);
if ($result === false) {
throw new RuntimeException("Unable to set session options");
}
$output = curl_exec($ch);
if ($output === false) {
throw new RuntimeException("Request failed: " . curl_error($ch));
}
curl_close($ch);
echo $output;
}
【问题讨论】:
-
您只提供一次 POST 字段。但是你有
CURLOPT_POSTFIELDS => $xml,和CURLOPT_POSTFIELDS => $id,;删除前者并将 $xm 放入 $id 数组中并传递它