Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Пример создания лидов в Bitrix24 из консольного php-скрипта
- Описание полей запроса к API Bitrix24 для создания лидов тут -> https://dev.1c-bitrix.ru/community/blogs/chaos/3980.php
- Данные для создания берем из БД, пример таблицы ниже
- DROP TABLE IF EXISTS `orders` ;
- CREATE TABLE `orders` (
- `id` int(12) NOT NULL AUTO_INCREMENT,
- `o_date` datetime NOT NULL,
- `o_url` varchar(255) NOT NULL,
- `o_fio` varchar(255) NOT NULL,
- `o_company` varchar(255) NOT NULL,
- `o_email` varchar(255) NOT NULL,
- `o_tel` varchar(255) NOT NULL,
- `o_status` int(1) NOT NULL DEFAULT '0',
- PRIMARY KEY (`id`),
- KEY `index_o_url` (`o_url`)
- ) ENGINE=InnoDB;
- INSERT INTO orders VALUES (null,'2017-03-24 14:12:23','http://programs74.ru/test1/','Admin','ARB','admin@a-r-b.ru','323232',0);
- INSERT INTO orders VALUES (null,'2017-03-24 14:23:11','http://programs74.ru/test2/','Vasua','Adobe','vasua@adobe.com','111111',0);
- INSERT INTO orders VALUES (null,'2017-03-24 15:11:17','http://programs74.ru/test3/','Petya','VMware','petya@vmware.com','555555',0);
- */
- $db_type = 'mysql';
- $db_host = 'localhost';
- $db_port = '3306';
- $db_user = 'root';
- $db_pass = 'XXXX';
- $db_name = 'test';
- $db_table_name = 'orders';
- $db_options = array();
- define('CRM_HOST', 'xxxx.bitrix24.ru');
- define('CRM_PORT', '443');
- define('CRM_PATH', '/crm/configs/import/lead.php');
- define('CRM_LOGIN', 'yyyyyy');
- define('CRM_PASSWORD', 'zzzzzz');
- // или токен авторизации
- //define('CRM_AUTH', 'XXXX');
- try {
- $dbh = new PDO("$db_type:host=$db_host;port=$db_port;dbname=$db_name", $db_user, $db_pass, $db_options);
- } catch (PDOException $e) {
- echo "\nPDO::errorInfo():\n";
- print $e->getMessage();
- }
- try {
- $query = "select * from $db_name.$db_table_name where o_status = 0;";
- $sth = $dbh->query($query);
- if (!$sth) {
- echo "\nPDO::errorInfo():\n";
- print_r($dbh->errorInfo());
- }
- while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
- $postData = array(
- 'TITLE' => $row['o_company'],
- 'COMPANY_TITLE' => $row['o_company'],
- 'NAME' => $row['o_fio'],
- 'PHONE_WORK' => $row['o_tel'],
- 'EMAIL_WORK' => $row['o_email'],
- 'WEB_OTHER' => $row['o_url'],
- 'STATUS_ID' => 'NEW',
- 'SOURCE_ID' => 'WEB',
- 'CURRENCY_ID' => 'RUB',
- 'COMMENTS' => $row['o_date']
- );
- 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);
- $query_up = "update $db_name.$db_table_name set o_status = '1' where id = '".$row['id']."';";
- $sth_up = $dbh->query($query_up);
- if (!$sth_up) {
- echo "\nPDO::errorInfo():\n";
- print_r($dbh->errorInfo());
- }
- $sth_up = NULL;
- }
- else
- {
- echo 'Connection Failed! '.$errstr.' ('.$errno.')';
- }
- }
- } catch (PDOException $e) {
- print $e->getMessage();
- }
- $sth = NULL;
- $dbh = NULL;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement