1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
bitrixservice.php
См. документацию.
1<?php
4
6{
7 const SERVICE_ID = "bitrixgeneric";
8
9 const AUTH_URL = "/oauth/authorize/";
10 const TOKEN_URL = "/oauth/token/";
11
12 const URL = '';
13
14 protected $scope = array();
15
16 protected $authResult = array();
17
18 public function __construct($appID = false, $appSecret = false, $code = false)
19 {
20 parent::__construct($appID, $appSecret, $code);
21 }
22
23 public function getResult()
24 {
25 return $this->authResult;
26 }
27
28 public function getError()
29 {
30 return is_array($this->authResult) && isset($this->authResult['error'])
31 ? $this->authResult
32 : '';
33 }
34}
35
37{
38 const SERVICE_URL = "/rest/";
39
40 const METHOD_METHODS = 'methods';
41 const METHOD_BATCH = 'batch';
42
43 protected $clientId = '';
44 protected $clientSecret = '';
46 protected ?int $streamTimeout = null;
47 protected bool $listenHttpErrors = false;
48
49 protected $serviceHost = '';
50
52 {
53 $this->clientId = $clientId;
54 $this->clientSecret = $clientSecret;
55 }
56
57 protected function setSeviceHost($host)
58 {
59 $this->serviceHost = $host;
60 }
61
62 protected function prepareAnswer($result)
63 {
64 return Json::decode($result);
65 }
66
67 public function call($methodName, $additionalParams = null, $licenseCheck = false)
68 {
69 global $APPLICATION;
70
71 if($this->clientId && $this->clientSecret)
72 {
73 if(!is_array($additionalParams))
74 {
75 $additionalParams = array();
76 }
77
78 $additionalParams['client_id'] = $this->clientId;
79 $additionalParams['client_secret'] = $this->clientSecret;
80
81 if($licenseCheck)
82 {
83 $additionalParams['key'] = static::getLicense();
84 }
85
86 $httpClientParams = [
87 'socketTimeout' => $this->httpTimeout,
88 ];
89
90 if ($this->streamTimeout)
91 {
92 $httpClientParams['streamTimeout'] = $this->streamTimeout;
93 }
94
95 $http = new HttpClient($httpClientParams);
96 $result = $http->post(
97 $this->serviceHost.static::SERVICE_URL.$methodName,
98 $additionalParams
99 );
100
101 $res = false;
102
103 try
104 {
105 $res = $this->prepareAnswer($result);
106 }
107 catch(\Bitrix\Main\ArgumentException $e)
108 {
109
110 }
111
112 if ($this->listenHttpErrors && !$res && $http->getError())
113 {
114 $res = [
115 'error' => $this->parseHttpError($http->getError())
116 ];
117 }
118
119 if($res)
120 {
121 if(!$licenseCheck && is_array($res) && isset($res['error']) && $res['error'] === 'verification_needed')
122 {
123 return $this->call($methodName, $additionalParams, true);
124 }
125 }
126 else
127 {
128 AddMessage2Log('Strange answer from Bitrix Service! '.$this->serviceHost.static::SERVICE_URL.$methodName.": ".$http->getStatus().' '.$result);
129 }
130
131 return $res;
132 }
133 else
134 {
135 throw new \Bitrix\Main\SystemException("No client credentials");
136 }
137 }
138
145 private function parseHttpError(array $errors): string
146 {
147 $errorsMsg = [];
148 foreach ($errors as $key => $value)
149 {
150 if (is_string($key))
151 {
152 $errorsMsg[] = "[{$key}] {$value}";
153 }
154 else
155 {
156 $errorsMsg[] = $value;
157 }
158 }
159
160 return implode(', ', $errorsMsg);
161 }
162
163 public function batch($actions)
164 {
165 $batch = array();
166
167 if(is_array($actions))
168 {
169 foreach($actions as $query_key => $arCmd)
170 {
171 list($cmd, $arParams) = array_values($arCmd);
172 $batch['cmd'][$query_key] = $cmd.(is_array($arParams) ? '?'.http_build_query($arParams) : '');
173 }
174 }
175
176 return $this->call(static::METHOD_BATCH, $batch);
177 }
178
179 public function getMethods()
180 {
181 return $this->call(self::METHOD_METHODS);
182 }
183
184 public function setTimeout($timeout)
185 {
186 $this->httpTimeout = $timeout;
187 }
188
189 public function setStreamTimeout(int $streamTimeout): void
190 {
191 $this->streamTimeout = $streamTimeout;
192 }
193
202 public function listenHttpErrors(bool $listen = true): static
203 {
204 $this->listenHttpErrors = $listen;
205
206 return $this;
207 }
208
209 protected static function getLicense()
210 {
211 return md5(LICENSE_KEY);
212 }
213}
$arParams
Определения access_dialog.php:21
global $APPLICATION
Определения include.php:80
Определения json.php:9
__construct($appID=false, $appSecret=false, $code=false)
Определения bitrixservice.php:18
const SERVICE_ID
Определения bitrixservice.php:7
setTimeout($timeout)
Определения bitrixservice.php:184
listenHttpErrors(bool $listen=true)
Определения bitrixservice.php:202
setSeviceHost($host)
Определения bitrixservice.php:57
const METHOD_METHODS
Определения bitrixservice.php:40
call($methodName, $additionalParams=null, $licenseCheck=false)
Определения bitrixservice.php:67
__construct($clientId, $clientSecret)
Определения bitrixservice.php:51
const METHOD_BATCH
Определения bitrixservice.php:41
int $streamTimeout
Определения bitrixservice.php:46
static getLicense()
Определения bitrixservice.php:209
setStreamTimeout(int $streamTimeout)
Определения bitrixservice.php:189
const SERVICE_URL
Определения bitrixservice.php:38
batch($actions)
Определения bitrixservice.php:163
bool $listenHttpErrors
Определения bitrixservice.php:47
prepareAnswer($result)
Определения bitrixservice.php:62
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$host
Определения .description.php:9
$errors
Определения iblock_catalog_edit.php:74
const LICENSE_KEY($show_sql_stat=='Y')
Определения start.php:84
AddMessage2Log($text, $module='', $traceDepth=6, $showArgs=false)
Определения tools.php:3941
$value
Определения Param.php:39
if(empty($signedUserToken)) $key
Определения quickway.php:257
const SOCSERV_DEFAULT_HTTP_TIMEOUT
Определения include.php:5