[av_codeblock wrapper_element=” wrapper_element_attributes=” codeblock_type=” av_uid=’av-kldkmxvn’ custom_class=’cust-codeblock-ads1 mb-small’]




[/av_codeblock]

[av_textblock size='' font_color='' color='' av-medium-font-size='' av-small-font-size='' av-mini-font-size='' av_uid='av-ypp5qk' custom_class='' admin_preview_bg='']
[av_breadcrumbs]
[/av_textblock]

[av_one_half first min_height='av-equal-height-column' vertical_alignment='av-align-middle' space='no_margin' custom_margin='aviaTBcustom_margin' margin='0px,50px' padding='0px' padding_sync='true' border='' border_color='' radius='0px' radius_sync='true' background_color='' src='' attachment='' attachment_size='' background_position='top left' background_repeat='no-repeat' animation='' mobile_display='' av_uid='av-ug9r58']
[av_image src='https://gestudio.dev/wp-content/uploads/2018/09/How-To-Add-Contact-To-VTiger-CRM-Using-VTiger-API_square_1.png' attachment='4428' attachment_size='full' align='center' styling='no-styling' hover='' link='' target='' caption='' font_size='' appearance='' overlay_opacity='0.4' overlay_color='#000000' overlay_text_color='#ffffff' animation='no-animation' custom_class='' admin_preview_bg='' av_uid='av-tn8nos'][/av_image]
[/av_one_half]

[av_one_half min_height='' vertical_alignment='' space='' custom_margin='' margin='0px' padding='0px' border='' border_color='' radius='0px' background_color='' src='' background_position='top left' background_repeat='no-repeat' animation='' mobile_display='' av_uid='av-rcj4bw']
[av_image src='https://gestudio.dev/wp-content/uploads/2020/11/scriptwriterph-technical-support-1.png' attachment='5957' attachment_size='full' align='center' styling='no-styling' hover='' link='page,3483' target='' caption='' font_size='' appearance='' overlay_opacity='0.4' overlay_color='#000000' overlay_text_color='#ffffff' copyright='' animation='no-animation' av_uid='av-qe0eoc' custom_class='' admin_preview_bg=''][/av_image]
[/av_one_half]

[av_textblock size='' font_color='' color='' av-medium-font-size='' av-small-font-size='' av-mini-font-size='' av_uid='av-ohqmqk' custom_class='' admin_preview_bg='']

How to Add Contact to VTiger CRM Using VTiger API

[wpb_last_updated_date]

Are you using VTiger CRM and need to connect to a third party application? It is often a requirement in a CRM to have the capability to talk to external systems. External systems are those system that isn’t hosted from your CRM server. A website hosted on a different server is a good example. In a website you commonly place your subscription forms, contact forms and other kind of forms. That’s actually a good business strategy to have external sources to gather more customers and to grow your network. How could you save data from external sources to your VTiger CRM database? Using what we call VTiger API, interconnection between your Vtiger CRM and third party applications becomes possible and easy.

You need to use VTiger API whether your syncing data or sharing information from a third party app to your VTiger CRM. Lucky us to have this VTiger CRM developers guide on third party app integration (REST APIs).
[/av_textblock]

[av_codeblock wrapper_element=” wrapper_element_attributes=” codeblock_type=” av_uid=’av-kldkmxvn’ custom_class=’cust-codeblock-ads1′]




[/av_codeblock]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” av_uid=’av-ohqmqk’ custom_class=” admin_preview_bg=”]
Before taking any action, we’re strongly suggesting you to backup your VTiger files and database first. It is also advisable to upgrade your VTiger CRM before integrating to a third party app for a better performance, improve security and fix bugs.

[av_hr class=’invisible’ height=’25’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ font=’entypo-fontello’ admin_preview_bg=” av_uid=’av-lny5fw’]

Requirements

  • Your VTiger CRM
  • Access to your VTiger CRM files

[av_hr class=’invisible’ height=’25’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ font=’entypo-fontello’ admin_preview_bg=” av_uid=’av-kgp0cc’]

Flowchart – How VTiger API Works

To demonstrate how VTiger API works we created a simple flowchart that will add contact details from external source to your VTiger CRM.

VTiger API add contact flow chart

PHP Code to Place within Your VTiger CRM

This is the source code to place within your VTiger CRM. Your third party app will call this script for them to be able to create contact on your VTiger CRM. Create folder api to your VTiger root directory. Create index.php file inside of api folder and paste the following code.
[/av_textblock]

[av_codeblock wrapper_element=’pre’ wrapper_element_attributes=” escape_html=’aviaTBescape_html’ deactivate_shortcode=’aviaTBdeactivate_shortcode’ deactivate_wrapper=’aviaTBdeactivate_wrapper’ av_uid=’av-fd1fd8′]
‘lastname’ => $lname,
’email’ => $email,
‘title’ => $title,
‘phone’ => $phone,
‘assigned_user_id’ => ‘administrator’
);

// validate POST data
if ( empty($vt_usr) || empty($vt_pw) || empty($vt_usr_key) ) {
$validated = false;
array_push($message, “Authentication failed”);
}

if ( (!preg_match(“/^[A-Za-z\- ‘]+$/”, $fname))
|| (empty($fname)) ) {
$validated = false;
array_push($message, “Invalid firstname”);
}

if ( (!preg_match(“/^[A-Za-z\- ‘]+$/”, $lname))
|| (empty($lname)) ) {
$validated = false;
array_push($message, “Invalid lastname”);
}

if ( (!filter_var($email, FILTER_VALIDATE_EMAIL))
|| (empty($email)) ) {
$validated = false;
array_push($message, “Invalid email”);
}

// is POST data validated ?
if ( $validated ) {

// Obtain CRM Token
$serveraddress = ‘http://springcrmv.crms.pro’;
$crm_username = $vt_usr;
$crm_userpassword = $vt_pw;
$crm_useraccesskey = $vt_usr_key;

$token_url = $serveraddress.”/webservice.php?operation=getchallenge&username=”.$crm_username;
$token_data = json_decode(file_get_contents($token_url));

if ($token_data->success != 1) die(‘Access denied’);

$crm_token = $token_data->result->token;

// Attempt Login
$service_url = $serveraddress.”/webservice.php”;
$curl = curl_init($service_url);
$curl_post_data = array(
‘operation’ => ‘login’,
‘username’ => $crm_username,
‘accessKey’ => md5($crm_token.$crm_useraccesskey),
);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);

$curl_response = json_decode(curl_exec($curl));

if ($curl_response->success != 1) die(‘Invalid user credentials’);

$crm_session = $curl_response->result->sessionName;

// Create Contact through API POST request
$curl1 = curl_init($service_url);

$objectJson = json_encode($params);
$moduleName = ‘Contacts’;
$curl_post_data1 = array(
‘sessionName’ => $crm_session,
‘operation’ => ‘create’,
‘element’ => $objectJson,
‘elementType’ => $moduleName,
);

curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_POST, true);
curl_setopt($curl1, CURLOPT_POSTFIELDS, $curl_post_data1);

$curl_response1 = json_decode(curl_exec($curl1));

array_push($message, “Success”);
}

// response to display
$response = array(
‘status’ => 200,
‘payload’ => $params,
‘input_validated’ => $validated,
‘message’ => $message
);

echo json_encode($response);
}
?>
[/av_codeblock]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” av_uid=’av-eacw70′ custom_class=” admin_preview_bg=”]
Source Code Explanation

Line 4: Determine whether the API request is a POST request.

Line 6: Get POST data from API request of the third party application.

Line 23: Initialize params (parameters) — data fields to be inserted to your VTiger CRM using VTiger API.

Line 33: Validate POST data received from API request.

Line 59: If all POST data are validated then the program will proceed on obtaining CRM token. CRM token is required when attempting to login to VTiger. Your login session is required when you’re trying to request to VTiger API.

Line 75: Attempt to authenticate VTiger credentials.

Line 94. Finally, call your VTiger web service with POST data. Contact details should be inserted to your VTiger CRM database.

[av_hr class=’invisible’ height=’25’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ font=’entypo-fontello’ admin_preview_bg=” av_uid=’av-cazvmk’]

PHP Code to Place within Your Third-party Application

This is the PHP script to place within your third-party app. This PHP script is responsible to call the first script that we have created.
[/av_textblock]

[av_codeblock wrapper_element=’pre’ wrapper_element_attributes=” escape_html=’aviaTBescape_html’ deactivate_shortcode=’aviaTBdeactivate_shortcode’ deactivate_wrapper=’aviaTBdeactivate_wrapper’ av_uid=’av-bhgcl8′]
‘firstname’ => ‘firstname’,
‘lastname’ => ‘Lastname’,
‘title’ => ‘Title’,
‘primary_email’ => ‘firstname.lastname@gmail.com’,
‘office_phone’ => ‘123-4567′,
);

// Call the PHP script that we have created earlier
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
curl_setopt($curl, CURLOPT_USERPWD, $username.’:’.$password);

// print the cURL response
echo “cURL Response
“;
echo ‘

';
	print_r(curl_exec($curl));
	echo '

‘;
?>
[/av_codeblock]

[av_codeblock wrapper_element=” wrapper_element_attributes=” codeblock_type=” av_uid=’av-kldkmxvn’ custom_class=’cust-codeblock-ads1′]




[/av_codeblock]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” av_uid=’av-8g30rg’ custom_class=” admin_preview_bg=”]
Source code explanation

Line 3: Initialize API URL — this is your VTiger CRM URL plus the location of the PHP script that we have created earlier.

Line 12: Initialize your VTiger CRM credentials and access token. You may store this kind of information to a database for security purposes. To get your access key login to your Vtiger CRM. Go to My Preferences, you should see your access key below.

Line 20: Prepare your contact details. This details can be from your website contact forms or subscription forms.

Line 29: Call the PHP script that we have created earlier. The script that we have created earlier will receive POST fields and will validate them. If POST data are all validated it will be inserted to your VTiger CRM Contacts.

Finally, Test VTiger API

For testing, you can use Postman to send a POST request with POST data.

To check if the script is working, login to your VTiger CRM and navigate to Contacts. You should see the newly created contact from your VTiger CRM.

We hope you make VTiger API works between your VTiger CRM and your third party app.

You may also read VTiger Dynamic Fields And Blocks Using Javascript/JQuery.
[/av_textblock]

[av_hr class=’default’ height=’50’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ av_uid=’av-7q9g98′]

[av_social_share title=’SHARE THIS POST’ style=’minimal’ buttons=” admin_preview_bg=” av_uid=’av-6hcxt8′]

[av_hr class=’invisible’ height=’50’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ font=’entypo-fontello’ admin_preview_bg=” av_uid=’av-4kkqak’]

[av_comments_list av_uid=’av-3623m4′]