I have a gravity form and I need to convert the submission to a xml string. The goal is to send the string to a third party url and create a customer and account; which I can manually do by copying and pasting the commented out(format needed) into my browser. Using the code below the xml is being created correctly (tested by printing), however when I echo the POST the return is Resource id #* (random #) and the echo of the results is xml parsing error. Does XML need to part of the whole curl function? I have tried changing the code to have the array convert to xml and then the post_to_url however when that seems to run on every page instead of being triggered by the gravity form submission.
<?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.thirdparty.com/api/lead/insertRecord?apiauthkey=apiauthkey=&secretkey=secretkey&xmlData=';
//This is the format needed: [url]https://www.thirdparty.com/api/lead/insertRecord?apiauthkey=apiauthkey=&secretkey=secretkey&xmlData=%3Ccrcloud%3E%3Clead%3E%3Ctype%3Eclient%3C/type%3E%3Cfirstname%3Eadam//%3C/firstname%3E%3Clastname%3Esmith%3C/lastname%3E%3C/lead%3E%3C/crcloud%3E[/url]
$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_RETURNTRANSFER, 1);
curl_setopt($post, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
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"]);
$xml = new SimpleXMLElement('<crcloud/>');
$lead = $xml->addchild('lead');
$data = array_flip($data);
array_walk_recursive($data, array ($xml, 'addChild'));
print $xml->asXML(); //ISCORRECT<crcloud><lead><type>client</type><firstname>adam</firstname><lastname>smith</lastname></lead></crcloud>
post_to_url($url, $xml);
}
?>