Created
September 15, 2021 09:29
-
-
Save webmypulse/a2b0935fd2f4887a434696abfb8ef5cb to your computer and use it in GitHub Desktop.
Интеграция CF7 c Bitrix24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Вызываем функцию для перехвата данных | |
| add_action( 'wpcf7_mail_sent', 'your_wpcf7_mail_sent_function' ); | |
| function your_wpcf7_mail_sent_function( $contact_form ) { | |
| // Подключаемся к серверу CRM | |
| define('CRM_HOST', 'b24-f3dziy.bitrix24.by'); // Ваш домен CRM системы | |
| define('CRM_PORT', '443'); // Порт сервера CRM. Установлен по умолчанию | |
| define('CRM_PATH', '/crm/configs/import/lead.php'); // Путь к компоненту lead.rest | |
| // Авторизуемся в CRM под необходимым пользователем: | |
| // 1. Указываем логин пользователя Вашей CRM по управлению лидами | |
| define('CRM_LOGIN', '[email protected]'); | |
| // 2. Указываем пароль пользователя Вашей CRM по управлению лидами | |
| define('CRM_PASSWORD', '01022013'); | |
| // Перехватываем данные из Contact Form 7 | |
| $title = $contact_form->title; | |
| $posted_data = $contact_form->posted_data; | |
| // Вместо "Контактная форма 1" необходимо указать название вашей контактной формы | |
| if ('Всплывающая форма' == $title ) { | |
| $submission = WPCF7_Submission::get_instance(); | |
| $posted_data = $submission->get_posted_data(); | |
| // Далее перехватываем введенные данные в полях Contact Form 7: | |
| // 1. Перехватываем поле [your-name] | |
| $firstName = $posted_data['wmp-name']; | |
| // 2. Перехватываем поле [your-message] | |
| $myphone = $posted_data['wmp-tel']; | |
| $nameform = $_POST['admin-data']; | |
| // Формируем параметры для создания лида в переменной $postData = array | |
| $postData = array( | |
| // Устанавливаем название для заголовка лида | |
| 'TITLE' => 'Заявка с формы ' . $nameform, | |
| 'NAME' => $firstName, | |
| 'PHONE_WORK' => $myphone, | |
| 'COMMENTS' => $nameform, | |
| ); | |
| // Передаем данные из Contact Form 7 в Bitrix24 | |
| if (defined('CRM_AUTH')) { | |
| $postData['AUTH'] = CRM_AUTH; | |
| } else { | |
| $postData['LOGIN'] = CRM_LOGIN; | |
| $postData['PASSWORD'] = CRM_PASSWORD; | |
| } | |
| $fp = fsockopen("ssl://".CRM_HOST, CRM_PORT, $errno, $errstr, 30); | |
| if ($fp) { | |
| $strPostData = ''; | |
| foreach ($postData as $key => $value) | |
| $strPostData .= ($strPostData == '' ? '' : '&').$key.'='.urlencode($value); | |
| $str = "POST ".CRM_PATH." HTTP/1.0\r\n"; | |
| $str .= "Host: ".CRM_HOST."\r\n"; | |
| $str .= "Content-Type: application/x-www-form-urlencoded\r\n"; | |
| $str .= "Content-Length: ".strlen($strPostData)."\r\n"; | |
| $str .= "Connection: close\r\n\r\n"; | |
| $str .= $strPostData; | |
| fwrite($fp, $str); | |
| $result = ''; | |
| while (!feof($fp)) | |
| { | |
| $result .= fgets($fp, 128); | |
| } | |
| fclose($fp); | |
| $response = explode("\r\n\r\n", $result); | |
| $output = '<pre>'.print_r($response[1], 1).'</pre>'; | |
| } else { | |
| echo 'Connection Failed! '.$errstr.' ('.$errno.')'; | |
| } | |
| } | |
| if ('Форма автор' == $title ) { | |
| $submission = WPCF7_Submission::get_instance(); | |
| $posted_data = $submission->get_posted_data(); | |
| // Далее перехватываем введенные данные в полях Contact Form 7: | |
| // 1. Перехватываем поле [your-name] | |
| $firstName = $posted_data['wmp-name']; | |
| // 2. Перехватываем поле [your-message] | |
| $myphone = $posted_data['wmp-tel']; | |
| // Формируем параметры для создания лида в переменной $postData = array | |
| $postData = array( | |
| // Устанавливаем название для заголовка лида | |
| 'TITLE' => 'Заявка с формы автор', | |
| 'NAME' => $firstName, | |
| 'PHONE_WORK' => $myphone, | |
| ); | |
| // Передаем данные из Contact Form 7 в Bitrix24 | |
| if (defined('CRM_AUTH')) { | |
| $postData['AUTH'] = CRM_AUTH; | |
| } else { | |
| $postData['LOGIN'] = CRM_LOGIN; | |
| $postData['PASSWORD'] = CRM_PASSWORD; | |
| } | |
| $fp = fsockopen("ssl://".CRM_HOST, CRM_PORT, $errno, $errstr, 30); | |
| if ($fp) { | |
| $strPostData = ''; | |
| foreach ($postData as $key => $value) | |
| $strPostData .= ($strPostData == '' ? '' : '&').$key.'='.urlencode($value); | |
| $str = "POST ".CRM_PATH." HTTP/1.0\r\n"; | |
| $str .= "Host: ".CRM_HOST."\r\n"; | |
| $str .= "Content-Type: application/x-www-form-urlencoded\r\n"; | |
| $str .= "Content-Length: ".strlen($strPostData)."\r\n"; | |
| $str .= "Connection: close\r\n\r\n"; | |
| $str .= $strPostData; | |
| fwrite($fp, $str); | |
| $result = ''; | |
| while (!feof($fp)) | |
| { | |
| $result .= fgets($fp, 128); | |
| } | |
| fclose($fp); | |
| $response = explode("\r\n\r\n", $result); | |
| $output = '<pre>'.print_r($response[1], 1).'</pre>'; | |
| } else { | |
| echo 'Connection Failed! '.$errstr.' ('.$errno.')'; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment