Forums

Send Form to x2 CRM and check if email is already registered

chuckers82 21 Aug, 2019
I have a form built with Chronoforms v5 on Joomla that I need to check an external CRM (http://wiki.x2crm.com/wiki/REST_API_Reference) if the email is already in the database the form will update that record, if that email is not in the database on the CRM it will create a new record. The CRM accepts CURL and right now I have it successfully create new records but ever form that is submitted creates a new record and does not check if it already exists. Attached is the code snippet the CRM guys sent me but I have no idea how to implement it into chronoforms.
// auth settings$url = 'domain name goes here/;$options = ['auth' => ['user' => ‘ADD USER NAME','pass' => ‘ADD PASSWORD',],'headers' => ['Content-Type: application/json'],'data' => null];// get the submitted email$email = urlencode($_POST['email']);// look it up on X2$json = get(“{$url}/Contacts/by:email={$email}.json?_useFirst=1”, $options);$response = json_decode($json, true);if ($response && !empty($response['id'])) {// email found// first set the data you want to change$options[‘data’] = [‘some_key’ => ‘some_value'];// update the existing recordpatch("{$url}/Contacts/{$id}.json”, $options);}else {// email NOT found// do what you’ve always done}// get functionpublic function get($url, $options=array()) {return curlRequest(‘GET’, $url, $options));}// patch functionpublic function patch($url, $options=array()) {return curlRequest(‘PATCH’, $url, $options));}// generic curl functionpublic function curlRequest($method, $url, $options=array()) {$h = curl_init();curl_setopt($h, CURLOPT_URL, $url);curl_setopt($h, CURLOPT_CUSTOMREQUEST, $method);curl_setopt($h, CURLOPT_RETURNTRANSFER, true);curl_setopt($h, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($h, CURLOPT_SSL_VERIFYHOST, false);// dataif (isset($options['data'])) {curl_setopt($h, CURLOPT_POSTFIELDS, $options['data']);}// headersif (isset($options['headers'])) {curl_setopt($h, CURLOPT_HEADER, array());curl_setopt($h, CURLOPT_HTTPHEADER,  $options['headers']);}// authif (isset($options['auth'])) {curl_setopt($h, CURLOPT_USERPWD, "{$options['auth']['user']}:{$options['auth']['pass']}");}// execute$response = curl_exec($h);$error = curl_error($h);$httpcode = curl_getinfo($h, CURLINFO_HTTP_CODE);}
I have the current form I can upload upon request.
healyhatman 26 Aug, 2019
// auth settings
$url = 'domain name goes here/;
$options = [
'auth' => ['user' => ‘ADD USER NAME','pass' => ‘ADD PASSWORD',],
'headers' => ['Content-Type: application/json'],
'data' => null];
// get the submitted email
$email = urlencode($form->data['emailfieldname']);
// look it up on X2
$json = get(“{$url}/Contacts/by:email={$email}.json?_useFirst=1”, $options);
$response = json_decode($json, true);
if ($response && !empty($response['id'])) {
// email found
// first set the data you want to change
$options[‘data’] = [‘some_key’ => ‘some_value'];
// update the existing record
patch("{$url}/Contacts/{$id}.json”, $options);
} else {
// email NOT found
// do what you’ve always done
}


// get function
public function get($url, $options=array()) {
return curlRequest(‘GET’, $url, $options));
}
// patch function
public function patch($url, $options=array()) {
return curlRequest(‘PATCH’, $url, $options));
}
// generic curl function
public function curlRequest($method, $url, $options=array()) {
$h = curl_init();
curl_setopt($h, CURLOPT_URL, $url);
curl_setopt($h, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
curl_setopt($h, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($h, CURLOPT_SSL_VERIFYHOST, false);
// data
if (isset($options['data'])) {
curl_setopt($h, CURLOPT_POSTFIELDS, $options['data']);
}// headers
if (isset($options['headers'])) {
curl_setopt($h, CURLOPT_HEADER, array());
curl_setopt($h, CURLOPT_HTTPHEADER,  
$options['headers']);
}
// auth
if (isset($options['auth'])) {
curl_setopt($h, CURLOPT_USERPWD, "{$options['auth']['user']}:{$options['auth']['pass']}");
}
// execute
$response = curl_exec($h);
$error = curl_error($h);
$httpcode = curl_getinfo($h, CURLINFO_HTTP_CODE);
}
chuckers82 26 Aug, 2019
That just throws a 404 error now on submit.
healyhatman 26 Aug, 2019
Make sure the email part is right I don't do cf5
chuckers82 26 Aug, 2019
I did. How much to make this beast be v6? I can send my backup for you to take a look.
healyhatman 26 Aug, 2019
I don't really have time to do private help anymore sorry you'd just have to figure it out. How hard it will be depends on how much custom code you have
This topic is locked and no more replies can be posted.