Advertisement
CHERTS

Пример создания лидов в Bitrix24 из php

Mar 24th, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.61 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Пример создания лидов в Bitrix24 из консольного php-скрипта
  5.  
  6. Описание полей запроса к API Bitrix24 для создания лидов тут -> https://dev.1c-bitrix.ru/community/blogs/chaos/3980.php
  7.  
  8. Данные для создания берем из БД, пример таблицы ниже
  9.  
  10. DROP TABLE IF EXISTS `orders` ;
  11. CREATE TABLE `orders` (
  12.   `id` int(12) NOT NULL AUTO_INCREMENT,
  13.   `o_date` datetime NOT NULL,
  14.   `o_url` varchar(255) NOT NULL,
  15.   `o_fio` varchar(255) NOT NULL,
  16.   `o_company` varchar(255) NOT NULL,
  17.   `o_email` varchar(255) NOT NULL,
  18.   `o_tel` varchar(255) NOT NULL,
  19.   `o_status` int(1) NOT NULL DEFAULT '0',
  20.   PRIMARY KEY (`id`),
  21.   KEY `index_o_url` (`o_url`)
  22. ) ENGINE=InnoDB;
  23. INSERT INTO orders VALUES (null,'2017-03-24 14:12:23','http://programs74.ru/test1/','Admin','ARB','admin@a-r-b.ru','323232',0);
  24. INSERT INTO orders VALUES (null,'2017-03-24 14:23:11','http://programs74.ru/test2/','Vasua','Adobe','vasua@adobe.com','111111',0);
  25. INSERT INTO orders VALUES (null,'2017-03-24 15:11:17','http://programs74.ru/test3/','Petya','VMware','petya@vmware.com','555555',0);
  26. */
  27.  
  28. $db_type = 'mysql';
  29. $db_host = 'localhost';
  30. $db_port = '3306';
  31. $db_user = 'root';
  32. $db_pass = 'XXXX';
  33. $db_name = 'test';
  34. $db_table_name = 'orders';
  35.  
  36. $db_options = array();
  37.  
  38. define('CRM_HOST', 'xxxx.bitrix24.ru');
  39. define('CRM_PORT', '443');
  40. define('CRM_PATH', '/crm/configs/import/lead.php');
  41. define('CRM_LOGIN', 'yyyyyy');
  42. define('CRM_PASSWORD', 'zzzzzz');
  43. // или токен авторизации
  44. //define('CRM_AUTH', 'XXXX');
  45.  
  46. try {
  47.     $dbh = new PDO("$db_type:host=$db_host;port=$db_port;dbname=$db_name", $db_user, $db_pass, $db_options);
  48. } catch (PDOException $e) {
  49.     echo "\nPDO::errorInfo():\n";
  50.     print $e->getMessage();
  51. }
  52.  
  53. try {
  54.     $query = "select * from $db_name.$db_table_name where o_status = 0;";
  55.     $sth = $dbh->query($query);
  56.     if (!$sth) {
  57.         echo "\nPDO::errorInfo():\n";
  58.         print_r($dbh->errorInfo());
  59.     }
  60.     while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
  61.  
  62.         $postData = array(
  63.             'TITLE' => $row['o_company'],
  64.             'COMPANY_TITLE' => $row['o_company'],
  65.             'NAME' => $row['o_fio'],
  66.             'PHONE_WORK' => $row['o_tel'],
  67.             'EMAIL_WORK' => $row['o_email'],
  68.             'WEB_OTHER' => $row['o_url'],
  69.             'STATUS_ID' => 'NEW',
  70.             'SOURCE_ID' => 'WEB',
  71.             'CURRENCY_ID' => 'RUB',
  72.             'COMMENTS' => $row['o_date']
  73.         );
  74.  
  75.         if (defined('CRM_AUTH'))
  76.         {
  77.             $postData['AUTH'] = CRM_AUTH;
  78.         }
  79.         else
  80.         {
  81.             $postData['LOGIN'] = CRM_LOGIN;
  82.             $postData['PASSWORD'] = CRM_PASSWORD;
  83.         }
  84.  
  85.         $fp = fsockopen("ssl://".CRM_HOST, CRM_PORT, $errno, $errstr, 30);
  86.         if ($fp)
  87.         {
  88.             $strPostData = '';
  89.  
  90.             foreach ($postData as $key => $value)
  91.                 $strPostData .= ($strPostData == '' ? '' : '&').$key.'='.urlencode($value);
  92.  
  93.             $str = "POST ".CRM_PATH." HTTP/1.0\r\n";
  94.             $str .= "Host: ".CRM_HOST."\r\n";
  95.             $str .= "Content-Type: application/x-www-form-urlencoded\r\n";
  96.             $str .= "Content-Length: ".strlen($strPostData)."\r\n";
  97.             $str .= "Connection: close\r\n\r\n";
  98.             $str .= $strPostData;
  99.             fwrite($fp, $str);
  100.             $result = '';
  101.             while (!feof($fp))
  102.             {
  103.                 $result .= fgets($fp, 128);
  104.             }
  105.             fclose($fp);
  106.  
  107.             $query_up = "update $db_name.$db_table_name set o_status = '1' where id = '".$row['id']."';";
  108.             $sth_up = $dbh->query($query_up);
  109.             if (!$sth_up) {
  110.                 echo "\nPDO::errorInfo():\n";
  111.                 print_r($dbh->errorInfo());
  112.             }
  113.             $sth_up = NULL;
  114.  
  115.         }
  116.         else
  117.         {
  118.             echo 'Connection Failed! '.$errstr.' ('.$errno.')';
  119.         }
  120.  
  121.     }
  122. } catch (PDOException $e) {
  123.     print $e->getMessage();
  124. }
  125.  
  126. $sth = NULL;
  127. $dbh = NULL;
  128.  
  129. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement