Jump to content

Help: Posting Gravity Form Data as xml to third party site


Adam_A

Recommended Posts

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 = 'url/api/lead/insertRecord?apiauthkey=apiauthkey=&secretkey=secretkey&xmlData=';

//This is the format needed: url/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%3Cemail%[email protected]%3C/email%3E%3Cphone-home%3E%28555%29555-5555%3C/phone-home%3E%3Cstreet_address//%3E555%20someplace%20Drive%3C/street_address%3E%3Ccity%3Esomewhere%3C/city%3E%3Cstate%3ETN%3C/state%3E%3Czip%3E12345%3C/zip%3E%3Cssno%3E987-45-6321%3C/ssno%3E//%3Cbirth_date%3E01/04/1960%3C/birth_date%3E%3Cclient_portal_access%3Eon%3C/client_portal_access%3E%3Cclient_userid%3Eadam1%3C/client_userid%3E%3Cclient_password//%3Eadams%3C/client_password%3E%3Cclient_sys_gen_uid_pass%3Eno%3C/client_sys_gen_uid_pass%3E%3Csend_login_info_via_email%3Eyes%3C/send_login_info_via_email%3E%3Cclient_assigned_to%3EAdam Smith%3C/client_assigned_to%3E%3C/lead%3E%3C/crcloud%3E


$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"],
"email" => $entry["19"],
"phone-home" => $entry["17"],
"street_address" => $entry["16.1"],
"city" => $entry["16.3"],
"state" => $entry["32"],
"zip" => $entry["16.5"],
"ssno" => $entry["11"],
"birth_date" => $entry["33"],
"client_portal_access" => $entry["27"],
"client_userid" => $entry["24"],
"client_password" => $entry["25"],
"client_sys_gen_uid_pass" => $entry["28"],
"send_login_info_via_email" => $entry["29"]);

$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><email>[email protected]</email><phone-//home>(555)555-5555</phone-home><street_address>555 someplace Drive</street_address><city>somewhere</city><state>TN</state><zip>12345</zip<ssno>987-45-6321</ssno><birth_date>01/04/1960</birth_date><client_portal_access>on</client_portal_access><client_userid>adam1</client_userid><client_password>adams</client_password><client_sys_gen_uid_pass>no</client_sys_gen_uid_pass><send_login_info_via_email>yes</send_login_info_via_email><client_assigned_to>Adam Smith</client_assigned_to></lead></crcloud>

post_to_url($url, $xml);

}

}

//Sending Another post to 2nd Third Party Site so already logged in after redirect
add_action('gform_after_submission', 'post_form', 10, 2);

function post_form($entry,$form){
$post_url = 'anotherurl';
$body = array(
'username' => $entry[24],
'password' => $entry[25],
);
send_form($post_url,$body);
}

//Send Form
function send_form($post_url,$body){
$request = new WP_Http();
$response = $request->post($post_url, array('body' => $body));
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.