Skip to content

Instantly share code, notes, and snippets.

@BrajeshKhare
Created October 14, 2016 05:16
Show Gist options
  • Save BrajeshKhare/56d7adde848dbfcae01013c08a4a2134 to your computer and use it in GitHub Desktop.
Save BrajeshKhare/56d7adde848dbfcae01013c08a4a2134 to your computer and use it in GitHub Desktop.
Gravity Forms integration for Hubspot and SugarCRM
<?php
/**
* Includes extensions for Gravity Forms to SugarCRM and Hubspot
* The demo code with all fields is located at the bottom of the file
* for future reference so we don't have to hunt it down later
*
* Don't forget to read the link on how to finish off the SugarCRM
* integration. I'll blog about it eventually so it's all in one
* spot.
*
* @since 1.0
*/
/**
* Including the Hubspot API Class
*/
locate_template( array( '/assets/includes/class.hapilead.php' ), true );
/**
* Including the SOAP SugarCRM toolkit
*/
locate_template( array( '/assets/includes/nusoap/lib/nusoap.php' ), true );
/**
* Wrapper class for SugarCRM Web Services
*
* @link http://kovshenin.com/2010/05/lead-generation-forms-with-wordpress-and-sugarcrm-2287/
*/
class SugarCRMWebServices {
// Let's define a place to store our access credentials
var $username;
var $password;
// We'll store the session ID here for later use
var $session;
// We'll initialize a new NuSOAP Client object into this one
var $soap;
// Constructor (PHP4-style)
function SugarCRMWebServices($username, $password)
{
// Copy the credentials into our containers and initialize NuSOAP
$this->username = $username;
$this->password = $password;
// Replace the URL with your copy of SugarCRM
$this->soap = new nusoap_client('younusoapclient');
}
todo fix the link above for the nusoap client
// Login function which stores our session ID
function login()
{
$result = $this->soap->call('login', array(
'user_auth' => array(
'user_name' => $this->username,
'password' => md5($this->password),
'version' => '.01'
),
'application_name' => 'My Application'));
$this->session = $result['id'];
}
// Create a new Lead, return the SOAP result
function createLead($data)
{
// Parse the data and store it into a name/value list
// which will then pe passed on to Sugar via SOAP
$name_value_list = array();
foreach($data as $key => $value)
array_push($name_value_list, array('name' => $key, 'value' => $value));
// Fire the set_entry call to the Leads module
$result = $this->soap->call('set_entry', array(
'session' => $this->session,
'module_name' => 'Leads',
'name_value_list' => $name_value_list
));
return $result;
}
}
/**
* Hubspot lead integration
*
* Integrates Hubspot in to the Gravity Forms. This integration is for form ID 4
* or partners page only.
*
* @since 1.0
*
*/
add_action( 'gform_after_submission_4', 'theme_t_wp_send_info_to_hubspot_partner', 10, 2 );
function theme_t_wp_send_info_to_hubspot_partner( $entry ){
//START HubSpot Lead Submission
$strPost = "";
//create string with form POST data
$strPost = "FirstName=" . urlencode( $entry[1] )
. "&LastName=" . urlencode( $entry[2] )
. "&Company=" . urlencode( $entry[3] )
. "&Website=" . urlencode( $entry[4] )
. "&Email=" . urlencode( $entry[5] )
. "&Phone=" . urlencode( $entry[6] )
. "&NumberOfClients=" . urlencode( $entry[7] )
. "&Message=" . urlencode( $entry[8] );
//set POST URL
$url = "http://hubspotformposturl.com/";
//intialize cURL and send POST data
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $strPost);
@curl_setopt($ch, CURLOPT_URL, $url);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@curl_exec($ch);
@curl_close($ch);
//END HubSpot Lead Submission
/* now for sugar */
$sugar = new SugarCRMWebServices( $your_username, $your_password );
// Login and create a new lead
$sugar->login();
$result = $sugar->createLead(array(
'lead_source' => 'Partners Form',
'lead_source_description' => 'Partners Form',
'lead_status' => 'New',
'first_name' => $entry[1],
'last_name' => $entry[2],
'account_name' => $entry[3],
'website' => $entry[4],
'email' => $entry[5],
'phone_work' => $entry[6],
'number_of_clients' => $entry[7],
'description' => $entry[8],
));
}
/**
*
* Below is simply the demo code from Hubspot so that we don't have to
* hunt it down later for all the fields we want.
*/
/*
//START HubSpot Lead Submission
$strPost = "";
//create string with form POST data
$strPost = "FirstName=" . urlencode($_POST['first_name'])
. "&LastName=" . urlencode($_POST['last_name'])
. "&Email=" . urlencode($_POST['email'])
. "&TwitterHandle=" . urlencode($_POST['twitter_handle'])
. "&Phone=" . urlencode($_POST['phone_number'])
. "&Fax=" . urlencode($_POST['fax_number'])
. "&Website=" . urlencode($_POST['website'])
. "&Company=" . urlencode($_POST['company'])
. "&JobTitle=" . urlencode($_POST['job_title'])
. "&Address=" . urlencode($_POST['address'])
. "&City=" . urlencode($_POST['city'])
. "&State=" . urlencode($_POST['state'])
. "&ZipCode=" . urlencode($_POST['zip_code'])
. "&Country=" . urlencode($_POST['country'])
. "&Message=" . urlencode($_POST['message'])
. "&NumberEmployees=" . urlencode($_POST['num_employees'])
. "&AnnualRevenue=" . urlencode($_POST['annual_revenue'])
. "&CloseDate=" . urlencode($_POST['close_date'])
. "&IPAddress=" . urlencode($_SERVER['REMOTE_ADDR'])
. "&UserToken=" . urlencode($_COOKIE['hubspotutk']);
//set POST URL
$url = "http://yoursite.hubspot.com/?app=leaddirector&FormName=demorequest";
//intialize cURL and send POST data
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $strPost);
@curl_setopt($ch, CURLOPT_URL, $url);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@curl_exec($ch);
@curl_close($ch);
//END HubSpot Lead Submission
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment