A business owner has a site on WordPress that has the same Gravity Forms form on every page where prospects can request a quote. Whenever someone submits a form, that information would be passed along and the customer would be logged as a prospect in his CRM.
When we contacted the CRM owner, he gave us some basic documentation for an API he has developed. It told us what URL to use and how to format our variables in an array. Using this post and the documentation that the CRM provided, I wrote the following and placed it in the WP child theme's functions.php file:
// Assigning variables to user input from the form; input_x is the "name" of each field; type is text for all
$fname = $_POST[‘input_1’];
$lname = $_POST[‘input_9’];
$email = $_POST[‘input_2’];
$phone = $_POST[‘input_3’];
$message = $_POST[‘input_5’];
//This was the API url provided by the CRM, I think it tells the code what it’s talking to?
$url="https://www.CRMwebsite.com/get_leads.php";
// Putting customer info into array that assigns data to the correct CRM fields for storage
$data = array (
“Username” => ‘Clients_CRM_username_goes_here’,
“First_Name” => $fname,
“Last_Name” => $lname,
“Email” => $email,
“Phone” => $phone,
“Notes” => $message,
);
// Everything below this was provided by the CRM so I didn’t touch it
$data=http_build_query($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
But when I submit a form, no new data appears in the CRM. I validated all the code here and didn't get any errors. I tried taking out the variables to see if that changed things but no dice. I also tried putting urlencode() around the phrases at the top (like: $fname = urlencode($_POST['input_1']) ). I have a few ideas of what may be the issue:
- The code above may need to be in a function of some sort or something else to let it know it needs to trigger on submission, not just floating in the functions.php file like it is right now... but I don't know where to start with that one
- Instead of using the field name in $_POST['input_1'], I may need to use a Gravity Forms get function to retrieve data or the field ID or something
- I may need to make the username $_POST['DudesUsername'] (doesn't seem right, but maybe)
- Something was sent incorrectly and, being unfamiliar with PHP, I can't see it (though unlikely since the validator didn't say anything)
But all of those things are slightly beyond my knowledge, so I wanted to ask before I spent all day in the docs trying to fix something that wasn't wrong in the first place. Thank you in advance for your help, sorry this is a long post!