【发布时间】:2014-02-19 18:50:13
【问题描述】:
我需要一个 gform 提交 POST 到第三方 api 作为 xmldata。
数据需要 POST 到的 URL:
需要的 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 数据直接粘贴到我的 url 后面并得到我想要的结果。
<?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);
}
}
?>
【问题讨论】:
标签: php xml wordpress post curl