我需要向第三方api提交gform提交POST作为xmldata。
数据需要POST到的网址:
需要的XML格式:
<crcloud>
<lead>
<type>Client</type>
<firstname>Scott</firstname>
<lastname>James</lastname>
<client_assigned_to>Bill Smith</client_assigned_to>
</lead>
</crcloud>
我写了一个curl函数,但是在发布时我收到了一个解析错误。我已经验证了xml,它是正确的。我还可以将直接打印出来的xml数据粘贴到我的网址后面并获得我想要的结果。
<?php
add_action("gform_after_submission", "set_post_content", 10, 2);
function set_post_content($entry, $form){
function post_to_url($url, $post) {
$fields = '';
foreach($post as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$url = 'https://www.3rdparty.com/api/lead/insertRecord?apiauthkey=yourapikey&secretkey=yoursecretkey&xmlData=yourxmldata';
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($post, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($post);
echo $post; //Resource id #*** currently
echo $result; //False 4404 XML parsing error currently
curl_close($post);
}
if($form["id"] == 2){//sign up now
$data = array(
"type" => $entry["31"],
"firstname" => $entry["3"],
"lastname" => $entry["4"],
"client_assigned_to" => $entry["34"]);
$xml = new SimpleXMLElement('<crcloud/>');
$lead = $xml->addchild('lead');
$data = array_flip($data);
array_walk_recursive($data, array ($xml, 'addChild'));
print $xml->asXML();
post_to_url($url, $xml);
}
}
?>
答案 0 :(得分:0)
添加此curl标头,让服务器知道您正在发送它们xml
curl_setopt($post, CURLOPT_HTTPHEADER,
array('Content-Type: application/xml')
);
在任何情况下,如果您没有发送XML数据(在您的示例中,我没有看到您发布xml)。这是:
$post_data = "<crcloud>
<lead>
<type>$client</type>
<firstname>$user</firstname>
<lastname>$name</lastname>
<client_assigned_to>$assigned</client_assigned_to>
</lead>
</crcloud>";
curl_setopt($post, CURLOPT_POSTFIELDS, $post_data);