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); } ?>

    Have you tried NOT sending the xml, but rather output it to something else, such as file or browser? Then taking the contents and run them through an xml validator? Such as a browser?

      johanafm;11038501 wrote:

      Have you tried NOT sending the xml, but rather output it to something else, such as file or browser? Then taking the contents and run them through an xml validator? Such as a browser?

      Is the purpose of this to determine if the xml is parsed correctly? As I stated I can manually take the outputted xml and paste the url in this format : https://www.thirdparty.com/api/lead/insertRecord?apiauthkey=apiauthkey=&secretkey=secretkey&xmlData=<crcloud><lead><type>client</type><firstname>adam</firstname><lastname>smith</lastname></lead></crcloud> : and it works like a charm; however I want this to be automated once the gform is submitted, with human interaction. I messed with zapier but all of the required fields are not available.

        aaugust;11038503 wrote:

        Is the purpose of this to determine if the xml is parsed correctly?

        Among other things. It is initially to verify that your xml is valid. If it is, then it could be used to determine if it is also parsed correctly.

          Don't urlencode the XML being added to the $fields array, as cURL will take care of that for you when you assign it to the CURLOPT_POSTFIELDS setting.

            Write a Reply...