Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

how do i customize this PHP code to work with gravity forms? WordPress

  • SOLVED

<?php
// Change the information below to work with your system
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

define(MAPPING_URL,'{mapping_url}'); // Change this url to the Web Post Integration Mapping URL

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// BEGIN - Form Post
if($_POST){
// Set Post URL
$url = MAPPING_URL;

// Map options To the correct Campaign Id
switch($_POST['input_6']){
case 'Radio Ad':
$campaign = '5'; // Edit Campaign Id here to change the campaign that Radio Ad goes into.
break;
case 'Television Ad':
$campaign = '7'; // Edit Campaign Id here to change the campaign that Television Ad goes into.
break;
case 'Online':
$campaign = '14'; // Edit Campaign Id here to change the campaign that Online goes into.
break;
case 'Other':
$campaign = '16'; // Edit Campaign Id here to change the campaign that Other goes into.
break;
default:
$campaign = '16'; // Edit Campaign Id here to change the default campaign.
}

// Set POST variables
$fields = array(
'input_1_3'=>urlencode($_POST['input_1_3']), // API: first_name - First Name
'input_1_6'=>urlencode($_POST['input_1_6']), // API: last_name - Last Name
'input_2_1'=>urlencode($_POST['input_2_1']), // API: cust_addr_550_addr1 - Address
'input_2_3'=>urlencode($_POST['input_2_3']), // API: cust_addr_550_city - City
'input_2_4'=>urlencode($_POST['input_2_4']), // API: cust_addr_550_state - State
'input_2_5'=>urlencode($_POST['input_2_5']), // API: cust_addr_550_zip - Zip
'input_2_6'=>urlencode($_POST['input_2_6']), // API: cust_addr_550_country - Country
'input_3'=>urlencode($_POST['input_3']), // API: phone - Phone
'input_4'=>urlencode($_POST['input_4']), // API: email - Email
'input_6'=>urlencode($campaign), // API: campaign_id - Campaign
'input_7'=>urlencode($_POST['input_7']), // API: customFields_406 - Referring URL
'lead_source'=>urlencode('Internet') // API: source - Lead Source
);

//url-ify the data for the POST
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
try{
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0);
//execute post
$_SESSION['lead_id'] = curl_exec($ch); // Set **Return Lead ID to 1 and **No HTML for Id Return(0-4) to 1 in the web post mapping to get the lead id
//close connection
curl_close($ch);
}catch(Exception $e){
error_log($e->getMessage());
}

}
// END - Form Post
?>

Answers (2)

2012-02-02

Christianto answers:

Hi,

Do you want to post gravity form data/entries to third party apps when the form submitted?
you can use [[LINK href="http://www.gravityhelp.com/documentation/page/Gform_post_submission"]]gform post submitted[[/LINK]] hook for this.

like:
add_action("gform_post_submission", "form_ads_submission", 10, 2);
or if you want to run only to specific form you can use the form id, for example execute only to form with id 2
add_action("gform_post_submission_2", "form_ads_submission", 10, 2);

then put your code inside the function, for example "form_ads_submission" function.

$entry object contain all data/value of the form, so using $entry['1'] mean get the first field, please check [[LINK href="http://www.gravityhelp.com/documentation/page/Entry_Object"]]the entry object [[/LINK]] and [[LINK href="http://www.gravityhelp.com/documentation/page/Form_Object"]]form object[[/LINK]] on gform documentation for more info. All form field order should match the entry object to be return.

so the function will be similar like this..
<?php

add_action("gform_post_submission_2", "form_ads_submission", 10, 2);

function form_ads_submission($entry, $form){

// Change the information below to work with your system
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
define(MAPPING_URL,'{mapping_url}'); // Change this url to the Web Post Integration Mapping URL
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// BEGIN - Form Post
if($entry){
// Set Post URL
$url = MAPPING_URL;

// Map options To the correct Campaign Id
switch($entry['10']){
case 'Radio Ad':
$campaign = '5'; // Edit Campaign Id here to change the campaign that Radio Ad goes into.
break;
case 'Television Ad':
$campaign = '7'; // Edit Campaign Id here to change the campaign that Television Ad goes into.
break;
case 'Online':
$campaign = '14'; // Edit Campaign Id here to change the campaign that Online goes into.
break;
case 'Other':
$campaign = '16'; // Edit Campaign Id here to change the campaign that Other goes into.
break;
default:
$campaign = '16'; // Edit Campaign Id here to change the default campaign.
}

// Set POST variables
$fields = array(
'input_1_3'=>urlencode($entry['1']), // API: first_name - First Name
'input_1_6'=>urlencode($entry['2']), // API: last_name - Last Name
'input_2_1'=>urlencode($entry['3']), // API: cust_addr_550_addr1 - Address
'input_2_3'=>urlencode($entry['4']), // API: cust_addr_550_city - City
'input_2_4'=>urlencode($entry['5']), // API: cust_addr_550_state - State
'input_2_5'=>urlencode($entry['6']), // API: cust_addr_550_zip - Zip
'input_2_6'=>urlencode($entry['7']), // API: cust_addr_550_country - Country
'input_3'=>urlencode($entry['8']), // API: phone - Phone
'input_4'=>urlencode($entry['9']), // API: email - Email
'input_6'=>urlencode($campaign), // API: campaign_id - Campaign
'input_7'=>urlencode($entry['11']), // API: customFields_406 - Referring URL
'lead_source'=>urlencode('Internet') // API: source - Lead Source
);

//url-ify the data for the POST
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}

try{
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0);
//execute post
$_SESSION['lead_id'] = curl_exec($ch); // Set **Return Lead ID to 1 and **No HTML for Id Return(0-4) to 1 in the web post mapping to get the lead id
//close connection
curl_close($ch);
}catch(Exception $e){
error_log($e->getMessage());
}

}
// END - Form Post

}


?>


Is this what you want?
hope this help..


Tamra Rich comments:

so this still isn't working for me, I get that i need to use the gravity forms hook, but i can't seem to figure out where to properly place the name and values of the entries so that they can be pushed.
<?php



add_action("gform_post_submission_1", "form_ads_submission", 10, 2);



function form_ads_submission($entry, $form){



// Change the information below to work with your system

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

// define(MAPPING_URL,'{subdomain}/do=noauth/add_lead/6'); // Change this url to the Web Post Integration Mapping URL

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////



// BEGIN - Form Post



// Set Post URL

$url = "http://{subdomain}/do=noauth/add_lead/6";



// Map options To the correct Campaign Id
if($entry){
switch($entry['6']){

case 'Radio Ad':

$campaign = '5'; // Edit Campaign Id here to change the campaign that Radio Ad goes into.

break;

case 'Television Ad':

$campaign = '7'; // Edit Campaign Id here to change the campaign that Television Ad goes into.

break;

case 'Online':

$campaign = '14'; // Edit Campaign Id here to change the campaign that Online goes into.

break;

case 'Other':

$campaign = '16'; // Edit Campaign Id here to change the campaign that Other goes into.

break;

default:

$campaign = '16'; // Edit Campaign Id here to change the default campaign.

}



// Set POST variables

$fields = array(

'first_name'=>urlencode($entry['1.3']), // API: first_name - First Name

'last_name'=>urlencode($entry['1.6']), // API: last_name - Last Name

'cust_addr_550_addr1'=>urlencode($entry['2.1']), // API: cust_addr_550_addr1 - Address

'cust_addr_550_city'=>urlencode($entry['2.3']), // API: cust_addr_550_city - City

'cust_addr_550_state'=>urlencode($entry['2.4']), // API: cust_addr_550_state - State

'cust_addr_550_addr_zip'=>urlencode($entry['2.5']), // API: cust_addr_550_zip - Zip

'cust_addr_550_country'=>urlencode($entry['2.6']), // API: cust_addr_550_country - Country

'phone'=>urlencode($entry['3']), // API: phone - Phone

'email'=>urlencode($entry['4']), // API: email - Email

'campaign_id'=>urlencode($campaign), // API: campaign_id - Campaign

'customFields_406'=>urlencode($entry['7']), // API: customFields_406 - Referring URL

'lead_source'=>urlencode('Internet') // API: source - Lead Source

);



//url-ify the data for the POST

foreach($fields as $key=>$value) {

$fields_string .= $key.'='.$value.'&';

}



try{

//open connection

$ch = curl_init();

//set the url, number of POST vars, POST data

curl_setopt($ch,CURLOPT_URL,$url);

curl_setopt($ch,CURLOPT_POST,count($fields));

curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0);

//execute post

$_SESSION['lead_id'] = curl_exec($ch); // Set **Return Lead ID to 1 and **No HTML for Id Return(0-4) to 1 in the web post mapping to get the lead id

//close connection

curl_close($ch);

}catch(Exception $e){

error_log($e->getMessage());

}



}

// END - Form Post



}


Tamra Rich comments:

or would this be a better way to go, I still can't figure out how to make this work, i must be missing something important. can anyone shed some light on what i'm missing?

add_action("gform_post_submission_1", "form_ads_submission", 10, 2);



function form_ads_submission($entry, $form){



// BEGIN - Form Post

if($entry){


// Set Post URL

$url = "http://{subdomaingoeshere}/do=noauth/add_lead/6";



// Map options To the correct Campaign Id
switch($entry['6']){

case 'Radio Ad':

$campaign = '5'; // Edit Campaign Id here to change the campaign that Radio Ad goes into.

break;

case 'Television Ad':

$campaign = '7'; // Edit Campaign Id here to change the campaign that Television Ad goes into.

break;

case 'Online':

$campaign = '14'; // Edit Campaign Id here to change the campaign that Online goes into.

break;

case 'Other':

$campaign = '16'; // Edit Campaign Id here to change the campaign that Other goes into.

break;

default:

$campaign = '16'; // Edit Campaign Id here to change the default campaign.

}



// Set POST variables

//START HubSpot Lead Submission
$strPost = "";
//create string with form POST data
$strPost = "first_name=" . urlencode( $entry[1.3] )
. "&last_name=" . urlencode( $entry[1.6] )
. "&cust_addr_550_addr1=" . urlencode( $entry[2.1] )
. "&cust_addr_550_city=" . urlencode( $entry[2.3] )
. "&cust_addr_550_state=" . urlencode( $entry[2.4] )
. "&cust_addr_550_zip=" . urlencode( $entry[2.5] )
. "&cust_addr_550_country=" . urlencode( $entry[2.6] )
. "&phone=" . urlencode( $entry[3] )
. "&email=" . urlencde ($entry[4])
."&campaign_id=" . urlencode ($campaign)
."&customFields_406=" . urlencode ($entry[7])
."&source=" . erlencode ('Internet');



//url-ify the data for the POST

foreach($strPost as $key=>$value) {

$strPost_string .= $key.'='.$value.'&';

}



//open connection

$ch = curl_init();

//set the url, number of POST vars, POST data

curl_setopt($ch,CURLOPT_URL,$url);

curl_setopt($ch,CURLOPT_POST,count($fields));

curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0);

//execute post

$_SESSION['lead_id'] = curl_exec($ch); // Set **Return Lead ID to 1 and **No HTML for Id Return(0-4) to 1 in the web post mapping to get the lead id

//close connection

curl_close($ch);



}

// END - Form Post

}


Christianto comments:

What kind of third party apps? is this lead generator?
Is there any documentation? I'm not sure about the value to be passed.

On above code you might want to delete:
//url-ify the data for the POST
foreach($strPost as $key=>$value) {
$strPost_string .= $key.'='.$value.'&';
}


and change:
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
to:
curl_setopt($ch,CURLOPT_POSTFIELDS,$strPost);


Christianto comments:

and this line should be urlencode right?
."&source=" . erlencode ('Internet');
become
."&source=" . urlencode ('Internet');

Or if you want to use previous code
add_action("gform_post_submission_1", "form_ads_submission", 10, 2);
function form_ads_submission($entry, $form){

// Change the information below to work with your system
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
define(MAPPING_URL,'{mapping_url}'); // Change this url to the Web Post Integration Mapping URL
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// BEGIN - Form Post
if($entry){
// Set Post URL
$url = "http://{subdomaingoeshere}/do=noauth/add_lead/6";

// Map options To the correct Campaign Id
switch($entry['6']){
case 'Radio Ad':
$campaign = '5'; // Edit Campaign Id here to change the campaign that Radio Ad goes into.
break;
case 'Television Ad':
$campaign = '7'; // Edit Campaign Id here to change the campaign that Television Ad goes into.
break;
case 'Online':
$campaign = '14'; // Edit Campaign Id here to change the campaign that Online goes into.
break;
case 'Other':
$campaign = '16'; // Edit Campaign Id here to change the campaign that Other goes into.
break;
default:
$campaign = '16'; // Edit Campaign Id here to change the default campaign.
}

// Set POST variables
$fields = array(
'first_name' =>urlencode($entry['1.3']), // API: first_name - First Name
'last_name' =>urlencode($entry['1.6']), // API: last_name - Last Name
'cust_addr_550_addr1' =>urlencode($entry['2.1']), // API: cust_addr_550_addr1 - Address
'cust_addr_550_city' =>urlencode($entry['2.3']), // API: cust_addr_550_city - City
'cust_addr_550_state' =>urlencode($entry['2.4']), // API: cust_addr_550_state - State
'cust_addr_550_zip' =>urlencode($entry['2.5']), // API: cust_addr_550_zip - Zip
'cust_addr_550_country'=>urlencode($entry['2.6']), // API: cust_addr_550_country - Country
'phone' =>urlencode($entry['3']), // API: phone - Phone
'email' =>urlencode($entry['4']), // API: email - Email
'campaign_id' =>urlencode($campaign), // API: campaign_id - Campaign
'customFields_406' =>urlencode($entry['1.7']), // API: customFields_406 - Referring URL
'source' =>urlencode('Internet') // API: source - Lead Source
);

//url-ify the data for the POST
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}

try{
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0);
//execute post
$_SESSION['lead_id'] = curl_exec($ch); // Set **Return Lead ID to 1 and **No HTML for Id Return(0-4) to 1 in the web post mapping to get the lead id
//close connection
curl_close($ch);
}catch(Exception $e){
error_log($e->getMessage());
}

}
// END - Form Post

}


Also, please check your $url, currently it just only string "subdomaingoeshere":
$url = "http://{subdomaingoeshere}/do=noauth/add_lead/6";


Tamra Rich comments:

thanks for all this insight, keep it coming :)

Yes this is for lead generation. There is more documentation on the thirdparty [[LINK href="http://community.insidesales.com/page/developers"]]here[[/LINK]].

I left the "subdomaingoeshere" so as not to make the url public, it is the correct url in my working code.


Christianto comments:

I check this[[LINK href="https://demo.insidesales.com/noauth/services/documentation?wsdl"]] documentation[[/LINK]] and I don't see any object with prefix "cust_addr_550" for example "cust_addr_550_addr1" for the lead. Only "addr1", test address "cust_addr_25_addr1" and test create address "cust_addr_34_addr1".

Instead of using cURL, have you tried by just use SOAPClient as describe on [[LINK href="http://community.insidesales.com/page/soap-documentation"]]this documentation[[/LINK]]?

I think you still have to supply username/password for the function, please try
[[LINK href="http://pastebin.com/ckm5Zhzd"]]http://pastebin.com/ckm5Zhzd[[/LINK]]

Is there any error on the log?


Christianto comments:

sorry, wrong curl header, you can delete this line on above code..
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($post_string) ));


Tamra Rich comments:

I have not tried it using the SOAP client and i am more unfamiliar with that. If you can show me how the code i already have would be changed to work in the SOAP then i will muck around with that.

What about the post mapping field names and variables from gravity forms? Do you know what the post mapping field name would be for field First Name? and what would it's corresponding post variable name be?


Christianto comments:

if we use soap:
[[LINK href="http://pastebin.com/PqHDZNt6"]]http://pastebin.com/PqHDZNt6[[/LINK]]

Field names and variables mapping for the post contain in $form object.
the [[LINK href="http://www.gravityhelp.com/documentation/page/Fields"]]fields object doc here[[/LINK]], you can check label, id, even cssClass:
print_r($form['fields']);
// get the fields value on $entry
$myforms = array();
foreach($form['fields'] as &$field) {
$myforms[] = 'id '.$field['id'].' = '.$entry[$field['id']];
}
print_r($myforms);


Is there any error on the log?
is hard to debug if we didn't know if the code run or not, or there are error respond form 3rd party apps.


Tamra Rich comments:

Christianto
I got the SOAP code for that form to work!! Thank you so much for all your help, I have a couple more forms that are a little more complicated, i would love for you to help me on them as well if it's at all possible. It's a similar situation with the other forms, where i have code i just need to make it work with gravity forms. I'll definitely tinker with it and with what you've shown me hopefully i can make them work, but if not would you be able to lend a hand?

2012-02-02

Arnav Joy answers:

what do you want to do ?


Tamra Rich comments:

I have a form on my website that i created with gravity forms. This code is supposed to automatically send the information collected from a form on my website to the CRM, but i would like it to work with the Gravity Forms form on my website. The form is located on the homepage of nineyearmortgage.com.