Created
June 25, 2020 11:13
-
-
Save luckydevilru/32ac906432d8c7fbef3ea33431e2477a to your computer and use it in GitHub Desktop.
Tilda bitrix24 crm utm метки лиды
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
| <? | |
| /** | |
| * Write data to log file. | |
| * | |
| * @param mixed $data | |
| * @param string $title | |
| * | |
| * @return bool | |
| */ | |
| function writeToLog($data, $title = '') { | |
| $log = "\n------------------------\n"; | |
| $log .= date("Y.m.d G:i:s") . "\n"; | |
| $log .= (strlen($title) > 0 ? $title : 'DEBUG') . "\n"; | |
| $log .= print_r($data, 1); | |
| $log .= "\n------------------------\n"; | |
| file_put_contents(getcwd() . '/hook.log', $log, FILE_APPEND); | |
| return true; | |
| } | |
| $defaults = array(); | |
| // working | |
| if ($_SERVER['REQUEST_METHOD'] == 'POST') { | |
| $leadData = $_POST['DATA']; | |
| $defaults = $_REQUEST; | |
| $comments = ''; | |
| foreach ($_POST['payment']['products'] as $key=>$value){ | |
| foreach ($value as $key=>$value){ | |
| if (is_array($value)){ | |
| foreach ($value['options'] as $key=>$value){ | |
| $comments .= $value['option'].": ".$value['variant']."<br>"; | |
| } | |
| }else{ | |
| $comments .= $key.": ".$value."<br>"; | |
| } | |
| } | |
| $comments .= "---<br>"; | |
| } | |
| if (!empty($comments)){ | |
| $comments .= "<br>Итого: ".$_POST['payment']['amount']." руб."; | |
| } | |
| $title = 'Новый Лид с '.$_SERVER['HTTP_HOST'].' №'.$_POST['payment']['orderid'] ?? ''; | |
| if (empty($title)){ | |
| $title = 'Лида с '.$_SERVER['HTTP_HOST'].' форма '.$_POST['formname']; | |
| } | |
| $source_id = array_search($utm_source, $source); | |
| writeToLog($_POST, 'webform'); | |
| $queryUrl = 'https://___CRM-ADDRESS___/rest/__USER-ID__/__CRM-TOKEN__/crm.lead.add.json'; | |
| $queryData = http_build_query(array( #указать номер чата - самому состоять в чатеy( | |
| 'fields' => array( | |
| 'TITLE' => $title, | |
| 'NAME' => $_POST['Name'] ?? '', | |
| 'ASSIGNED_BY_ID' => __USER-ID__, // ОТВЕТСВЕННЫЙ | |
| 'UTM_TERM' => $_POST['utm_term'] ?? '', | |
| 'UTM_SOURCE' => $_POST['utm_source'] ?? '', | |
| 'UTM_MEDIUM' => $_POST['utm_medium'] ?? '', | |
| 'HTTP_HOST' => $_SERVER['HTTP_HOST'] ?? '', | |
| 'UTM_CAMPAIGN' => $_POST['utm_campaign'] ?? '', | |
| 'UTM_CONTENT' => $_POST['utm_content'] ?? '', | |
| 'UTM_TERM' => $_POST['utm_term'] ?? '', | |
| 'OPPORTUNITY'=> $_POST['payment']['amount'] ?? '', | |
| "STATUS_ID" => "NEW", | |
| 'COMMENTS' => $comments ?? '', | |
| "STATUS_ID" => "NEW", | |
| "OPENED" => "Y", // ДОСТУПЕН ВСЕМ | |
| "PHONE" => array( | |
| array( | |
| "VALUE" => $_POST['Phone'] ?? '', | |
| "VALUE_TYPE" => "WORK" | |
| ) | |
| ), | |
| "SOURCE_ID" => $source_id, | |
| "EMAIL" => array( | |
| array( | |
| "VALUE" => $_POST['Email'] ?? '', | |
| "VALUE_TYPE" => "WORK" | |
| ) | |
| ), | |
| ), | |
| 'params' => array( | |
| "REGISTER_SONET_EVENT" => "Y" | |
| ) | |
| )); | |
| $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); | |
| writeToLog($result, 'webform result'); | |
| if (array_key_exists('error', $result)) | |
| echo "Ошибка при сохранении лида: " . $result['error_description'] . ""; | |
| // УВЕДОМЛЕНИЕ В ЧАТ О НОВОМ ЛИДЕ | |
| // $queryNotice = 'https://___CRM-ADDRESS___/rest/__USER-ID__/__CRM-TOKEN__/im.message.add'; | |
| // $Data = http_build_query(array( | |
| // 'DIALOG_ID' => "chat57", #указать номер чата - самому состоять в чате | |
| // 'MESSAGE' => "Новый лид, Ребята! проверьте!", | |
| // )); | |
| // $curl = curl_init(); | |
| // curl_setopt_array($curl, array( | |
| // CURLOPT_SSL_VERIFYPEER => 0, | |
| // CURLOPT_POST => 1, | |
| // CURLOPT_HEADER => 0, | |
| // CURLOPT_RETURNTRANSFER => 1, | |
| // CURLOPT_URL => $queryNotice, | |
| // CURLOPT_POSTFIELDS => $Data | |
| // )); | |
| // $result = curl_exec($curl); | |
| // curl_close($curl); | |
| // $result = json_decode($result, 1); | |
| // // var_dump($result); | |
| // // writeToLog($result, 'webform result'); | |
| // if (array_key_exists('error', $result)) | |
| // var_dump($result); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment