【问题标题】:Need to POST xml string to 3rd party api, using gravity forms需要使用重力形式将 xml 字符串发布到 3rd 方 api
【发布时间】:2014-02-19 18:50:13
【问题描述】:

我需要一个 gform 提交 POST 到第三方 api 作为 xmldata。

数据需要 POST 到的 URL:

https://www.3rdparty.com/api/lead/insertRecord?apiauthkey=yourapikey&secretkey=yoursecretkey&xmlData=yourxmldata

需要的 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


    【解决方案1】:

    添加此 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);
    

    【讨论】:

    • 仍然出错,有没有办法验证发送的确切内容以确保格式正确?
    • 是的,您可以使用详细选项来检查正在传输的内容。 curl_setopt($post, CURLOPT_VERBOSE, true);
    • 还要检查我更新的答案。它显示了您需要如何发送 xml 数据。只需使用您的帖子参数制作 xml。
    • 我仍在处理这个问题,它发送的有效负载是 373 个字符,但我仍然无法查看实际有效负载以查看它是否尝试发送 xml,我相信我越来越接近了.关于 xml,我将其转换为 xml,如下所示:$xml = new SimpleXMLElement(''); $lead = $xml->addchild('lead'); $data = array_flip($data); array_walk_recursive($data, array ($xml, 'addChild'));然后尝试发布: post_to_url($url, $xml);
    猜你喜欢
    • 2016-11-15
    • 2021-01-10
    • 2015-10-21
    • 1970-01-01
    • 2022-01-18
    • 2015-07-27
    • 2012-12-21
    • 2016-08-02
    • 2017-09-10
    相关资源
    最近更新 更多