The last notes
All English-language materials have been translated fully automatically using the Google service
Programmatically adding a lead to Bitrix24 using direct authorization
//формируем массив для передачи в bitrix24
$crmUrl = 'https://MYCRM.bitrix24.ru/';
$arParams = array(
'LOGIN' => 'MY@MAIL.RU', // обязательно, логин для доступа к crm
'PASSWORD' => 'MY_PASSWORD', // обязательно, пароль для доступа к crm
'TITLE' => $data['TITLE'], // обязательно, название лида
"SOURCE_ID" => 'WEB',
"UTM_SOURCE" => $data['UTM_SOURCE'],
"NAME" => $data['NAME'] ? $data['NAME'] : 'Случайное имя',
'ASSIGNED_BY_ID' => 19, // Ответственный
"EMAIL" => [["VALUE" => $data['EMAIL'], "VALUE_TYPE" => "WORK"]],
"PHONE" => [["VALUE" => $data['PHONE'], "VALUE_TYPE" => "WORK"]],
"COMMENTS" => $data['COMMENT'] ? "Лид создан автоматически; ".$data['COMMENT'] : "Лид создан автоматически",
"UF_CRM_1581408073" => $data['DATE'], //дата модификации лида
);
$obHttp = new \Bitrix\Main\Web\HttpClient;
$result = $obHttp->post($crmUrl.'crm/configs/import/lead.php', $arParams);
$result = json_decode(str_replace('\'', '"', $result), true);
return $result['error_message'];
For some time now, Bitrix24 requires an active commercial subscription to use webhooks. I think this decision is controversial and therefore I do not advise using this functionality of Bitrix24.
Obtaining the authorization code of the Bitrix24 Webhook API
Log in to Bitrix24 and go to the address: Applications -> Webhooks
. Click on Add webhook
, select the type external
We set the necessary access. In our case, this is CRM
and save. The page will refresh and you will receive an authorization code and an API URL. Save this data and do not show it to anyone. It looks like this:
Please note that you need to remove the ending profile/
from the URL to call the REST API
Programmatically adding a lead using the Bitrix24 Webhook API
public function leadCreate ($ data) {
// create an array for transmission to bitrix24
// we take the data from the element of the added infoblock
$ queryData = http_build_query (array (
'fields' => array (
"TITLE" => 'Lead from the site' .SITE_SERVER_NAME. ' from the form "Find out the wholesale price". ', // Lead title
"SOURCE_ID" => 'WEB', // Lead source
"UTM_SOURCE" => $ data ['UTM_SOURCE'], // UTM label
"NAME" => $ data ['NAME']? $ data ['NAME']: 'Random name', // Contact name
'ASSIGNED_BY_ID' => 19, // Responsible, where the user id of your Bitrix24 is required
"EMAIL" => [["VALUE" => $ data ['EMAIL'], "VALUE_TYPE" => "WORK"]], // Contact mail
"PHONE" => [["VALUE" => $ data ['PHONE'], "VALUE_TYPE" => "WORK"]], // Contact phone
"COMMENTS" => "Lead was created automatically from the site." SITE_SERVER_NAME, // Comment
"UF_CRM_1581408073" => $ data ['DATE'], // Filling in the custom field
),
'params' => array ("REGISTER_SONET_EVENT" => "Y"), // We say that we need to register a new event and notify all
));
// refer to Bitrix24 using the curl_exec function
// method crm.lead.add.json adds lead
$ rest = 'crm.lead.add.json';
// url is taken from the created webhook, removing the ending prifile /
// and adding the $ rest method to add a lead
$ queryUrl = 'https: //YOUR_BITRIX24_ADRESS/rest/593/YOUR_API_CODE/'.$rest;
$ curl = curl_init ();
curl_setopt_array ($ curl, array (
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $ queryUrl,
CURLOPT_POSTFIELDS => $ queryData,
));
$ result = curl_exec ($ curl);
curl_close ($ curl);
$ result = json_decode ($ result, 1);
if (array_key_exists ('error', $ result))
{
echo "Error saving lead:". $ result ['error_description']. "";
}
else
{
echo $ result ['result'];
}
}
Programmatically adding a lead using the Bitrix24 Webhook API
public function leadCreate ($ data) {
// create an array for transmission to bitrix24
// we take the data from the element of the added infoblock
$ queryData = http_build_query (array (
'fields' => array (
"TITLE" => 'Lead from the site' .SITE_SERVER_NAME. ' from the form "Find out the wholesale price". ', // Lead title
"SOURCE_ID" => 'WEB', // Lead source
"UTM_SOURCE" => $ data ['UTM_SOURCE'], // UTM label
"NAME" => $ data ['NAME']? $ data ['NAME']: 'Random name', // Contact name
'ASSIGNED_BY_ID' => 19, // Responsible, where the user id of your Bitrix24 is required
"EMAIL" => [["VALUE" => $ data ['EMAIL'], "VALUE_TYPE" => "WORK"]], // Contact mail
"PHONE" => [["VALUE" => $ data ['PHONE'], "VALUE_TYPE" => "WORK"]], // Contact phone
"COMMENTS" => "Lead was created automatically from the site." SITE_SERVER_NAME, // Comment
"UF_CRM_1581408073" => $ data ['DATE'], // Filling in the custom field
),
'params' => array ("REGISTER_SONET_EVENT" => "Y"), // We say that we need to register a new event and notify all
));
// refer to Bitrix24 using the curl_exec function
// method crm.lead.add.json adds lead
$ rest = 'crm.lead.add.json';
// url is taken from the created webhook, removing the ending prifile /
// and adding the $ rest method to add a lead
$ queryUrl = 'https: //YOUR_BITRIX24_ADRESS/rest/593/YOUR_API_CODE/'.$rest;
$ curl = curl_init ();
curl_setopt_array ($ curl, array (
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $ queryUrl,
CURLOPT_POSTFIELDS => $ queryData,
));
$ result = curl_exec ($ curl);
curl_close ($ curl);
$ result = json_decode ($ result, 1);
if (array_key_exists ('error', $ result))
{
echo "Error saving lead:". $ result ['error_description']. "";
}
else
{
echo $ result ['result'];
}
}
Filtering leads by phone number using Bitrix24 Webhook API
If we do everything correctly, then before adding a lead, we need to make sure that this phone number is not yet among the leads
Therefore, we first filter leads
public function leadSearchByPhone ($ data) {// Get a list of leads filtered by phone number
$ queryData = http_build_query (array (
'filter' => array (
"PHONE" => $ data ['PHONE'],
)
));
$ rest = 'crm.lead.list';
$ queryUrl = 'https: //YOUR_BITRIX24_ADRESS/rest/593/YOUR_API_CODE/'.$rest;
$ curl = curl_init ();
curl_setopt_array ($ curl, array (
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $ queryUrl,
CURLOPT_POSTFIELDS => $ queryData,
));
$ result = curl_exec ($ curl);
curl_close ($ curl);
$ result = json_decode ($ result);
return $ result;
}
This will return an array. In which, if leads were found, then $ result ['result']
will contain the list of leads we need
Programmatically getting a lead by id using Bitrix24 Webhook API
public function leadGetById ($ data) {// Get the required lead fields by ID
$ queryData = http_build_query (array (
'id' => $ data ['ID']
));
$ rest = 'crm.lead.get';
$ queryUrl = 'https: //YOUR_BITRIX24_ADRESS/rest/593/YOUR_API_CODE/'.$rest;
$ curl = curl_init ();
curl_setopt_array ($ curl, array (
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $ queryUrl,
CURLOPT_POSTFIELDS => $ queryData,
));
$ result = curl_exec ($ curl);
curl_close ($ curl);
$ result = json_decode ($ result);
return $ result;
}
Software update of a lead by id using Bitrix24 Webhook API
public function leadUpdateById ($ data) {
$ queryData = http_build_query (array (
'id' => $ data ['ID'],
'fields' => array (
"NAME" => $ data ['NAME'],
"UTM_SOURCE" => $ data ['UTM_SOURCE'], // UTM label
"COMMENTS" => "Lead was created automatically from the site" .SITE_SERVER_NAME. "- repeated request", // Comment
"STATUS_ID" => "NEW", // Update the lead status
"DATE_MODIFY" => $ data ['DATE'], // Update the standard modification date field
"UF_CRM_1581408073" => $ data ['DATE'], // Filling in the custom field
),
'params' => array ("REGISTER_SONET_EVENT" => "Y"), // We say that we need to register a new event and notify all
));
$ rest = 'crm.lead.update';
$ queryUrl = 'https: //YOUR_BITRIX24_ADRESS/rest/593/YOUR_API_CODE/'.$rest;
$ curl = curl_init ();
curl_setopt_array ($ curl, array (
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $ queryUrl,
CURLOPT_POSTFIELDS => $ queryData,
));
$ result = curl_exec ($ curl);
curl_close ($ curl);
$ result = json_decode ($ result);
return $ result;
}
Full listing of a script for checking the existence of a lead, registering a new lead and updating an existing lead
First of all, let's agree that a POST request of the following form comes to our file:
Array
(
[sessid] => 8fa4b79357bcd2a58bb6614996fce466
[NAME] => Test name
[PHONE] => +7 (000) 000-000
[FAIL_STATE] =>
[EMAIL] => 123@123.com
[AJAX] => Y
[ACTION] => REQUEST_FORM
[UTM_SOURCE] => 'some_source'
)
Then the code of our file will be like this:
require_once ($ _ SERVER ['DOCUMENT_ROOT']. '/ bitrix / modules / main / include / prolog_before.php');
class bitrix24 {
public function leadCreate ($ data) {
// create an array for transmission to bitrix24
// we take the data from the element of the added infoblock
$ queryData = http_build_query (array (
'fields' => array (
"TITLE" => 'Lead from the site' .SITE_SERVER_NAME. ' from the form "Find out the wholesale price". ', // Lead title
"SOURCE_ID" => 'WEB', // Lead source
"UTM_SOURCE" => $ data ['UTM_SOURCE'], // UTM label
"NAME" => $ data ['NAME']? $ data ['NAME']: 'Random name', // Contact name
'ASSIGNED_BY_ID' => 19, // Responsible, where the user id of your Bitrix24 is required
"EMAIL" => [["VALUE" => $ data ['EMAIL'], "VALUE_TYPE" => "WORK"]], // Contact mail
"PHONE" => [["VALUE" => $ data ['PHONE'], "VALUE_TYPE" => "WORK"]], // Contact phone
"COMMENTS" => $ data ['COMMENT']? "Lead was created automatically from the site" .SITE_SERVER_NAME. '; User comment: '. $ Data [' COMMENT ']: "Lead was created automatically from the site" .SITE_SERVER_NAME,
"UF_CRM_1581408073" => $ data ['DATE'], // Filling in the custom field
),
'params' => array ("REGISTER_SONET_EVENT" => "Y"), // We say that we need to register a new event and notify all
));
// refer to Bitrix24 using the curl_exec function
// method crm.lead.add.json adds lead
$ rest = 'crm.lead.add.json';
// url is taken from the created webhook, removing the ending prifile /
// and adding the $ rest method to add a lead
$ queryUrl = 'https: //YOUR_BITRIX24_ADRESS/rest/593/YOUR_API_CODE/'.$rest;
$ curl = curl_init ();
curl_setopt_array ($ curl, array (
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $ queryUrl,
CURLOPT_POSTFIELDS => $ queryData,
));
$ result = curl_exec ($ curl);
curl_close ($ curl);
$ result = json_decode ($ result, 1);
if (array_key_exists ('error', $ result))
{
echo "Error saving lead:". $ result ['error_description']. "";
}
else
{
echo $ result ['result'];
}
}
public function leadSearchByPhone ($ data) {// Get the list of leads filtered by phone number
$ queryData = http_build_query (array (
'filter' => array (
"PHONE" => $ data ['PHONE'],
)
));
$ rest = 'crm.lead.list';
$ queryUrl = 'https: //YOUR_BITRIX24_ADRESS/rest/593/YOUR_API_CODE/'.$rest;
$ curl = curl_init ();
curl_setopt_array ($ curl, array (
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $ queryUrl,
CURLOPT_POSTFIELDS => $ queryData,
));
$ result = curl_exec ($ curl);
curl_close ($ curl);
$ result = json_decode ($ result);
return $ result;
}
public function leadGetById ($ data) {// Get the fields of the required lead by ID
$ queryData = http_build_query (array (
'id' => $ data ['ID']
));
$ rest = 'crm.lead.get';
$ queryUrl = 'https: //YOUR_BITRIX24_ADRESS/rest/593/YOUR_API_CODE/'.$rest;
$ curl = curl_init ();
curl_setopt_array ($ curl, array (
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $ queryUrl,
CURLOPT_POSTFIELDS => $ queryData,
));
$ result = curl_exec ($ curl);
curl_close ($ curl);
$ result = json_decode ($ result);
return $ result;
}
public function leadUpdateById ($ data) {
$ queryData = http_build_query (array (
'id' => $ data ['ID'],
'fields' => array (
"NAME" => $ data ['NAME'],
"UTM_SOURCE" => $ data ['UTM_SOURCE'], // UTM label
"COMMENTS" => 'Lead was created automatically from the site' .SITE_SERVER_NAME. ' - repeated appeal; User comment: '. $ Data [' COMMENT '], // Comment
"STATUS_ID" => "NEW", // Update the lead status
"DATE_MODIFY" => $ data ['DATE'], // Update the standard modification date field
"UF_CRM_1581408073" => $ data ['DATE'], // Filling in the custom field
),
'params' => array ("REGISTER_SONET_EVENT" => "Y"), // We say that we need to register a new event and notify all
));
$ rest = 'crm.lead.update';
$ queryUrl = 'https: //YOUR_BITRIX24_ADRESS/rest/593/YOUR_API_CODE/'.$rest;
$ curl = curl_init ();
curl_setopt_array ($ curl, array (
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $ queryUrl,
CURLOPT_POSTFIELDS => $ queryData,
));
$ result = curl_exec ($ curl);
curl_close ($ curl);
$ result = json_decode ($ result);
return $ result;
}
}
if (check_bitrix_sessid () && $ _SERVER ["REQUEST_METHOD"] == "POST" && $ _POST ["ACTION"] == "REQUEST_FORM") {
if (htmlspecialchars ($ _ POST ['FAIL_STATE'])) {
echo json_encode (array ('success' => 'Y')); // Check if the bot has filled in a hidden field. If the fields are filled in, we do nothing
} else {
// To adjust the server time and your local time
// For Novosibirsk, for example, I use the following construction: time () + (4 * 60 * 60)
//Those. add 4 hours to the current time
$ added_time = time () + (1 * 60 * 60);
$ date = date ('Y-m-d H: i: s', $ added_time);
$ bitrix24 = new bitrix24;
$ data = array (
'NAME' => htmlspecialchars ($ _ POST ['NAME']),
'PHONE' => preg_replace ("/ [^ 0-9] /", '', htmlspecialchars ($ _ POST ['PHONE'])), // Leave only numbers
'EMAIL' => htmlspecialchars ($ _ POST ['EMAIL']),
'UTM_SOURCE' => htmlspecialchars ($ _ POST ['UTM_SOURCE']),
'DATE' => $ date,
);
$ lead = json_decode (json_encode ($ bitrix24-> leadSearchByPhone ($ data)), True);
if (count ($ lead ['result']) <1) {// If no leads found
echo json_encode ($ bitrix24-> leadCreate ($ data)); // create a lead
} else {
$ data ['ID'] = $ lead ['result'] [0] ['ID']; // Get the id of the first lead
echo json_encode ($ bitrix24-> leadUpdateById ($ data)); // Update the lead by id
}
}
}
Comments