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