19if(!defined(
"BITRIX_OAUTH_URL"))
21 $defaultValue = \Bitrix\Main\Config\Option::get(
'rest',
'oauth_server',
'https://oauth.bitrix.info');
22 define(
"BITRIX_OAUTH_URL", $defaultValue);
25if(!defined(
'BITRIXREST_URL'))
27 define(
'BITRIXREST_URL', BITRIX_OAUTH_URL);
59 private const NAME_IDENTIFIER_REQUEST =
'bx24_request_id';
74 if(!is_array($additionalParams))
76 $additionalParams = array();
80 $additionalParams = Encoding::convertEncoding($additionalParams, LANG_CHARSET,
"utf-8");
83 return $additionalParams;
93 $additionalParams[
'member_id'] = \CRestUtil::getMemberId();
97 $additionalParams = \CRestUtil::signLicenseRequest($additionalParams, $this->licenseKey);
100 return $additionalParams;
107 return Json::decode($result);
118 'socketTimeout' => static::HTTP_SOCKET_TIMEOUT,
119 'streamTimeout' => static::HTTP_STREAM_TIMEOUT,
126 if (isset($_SERVER[
'BX24_REQUEST_ID']))
128 if (str_contains($methodName,
'?'))
137 $requestId = $separator . http_build_query([
138 static::NAME_IDENTIFIER_REQUEST => urlencode($_SERVER[
'BX24_REQUEST_ID'])
147 return static::SERVICE_URL . static::SERVICE_PATH . $methodName . $this->
getRequestId($methodName);
161 public function call($methodName, $additionalParams =
null, $licenseCheck =
false)
163 if ($this->clientId && $this->clientSecret)
165 $additionalParams = $this->
prepareRequest($additionalParams, $licenseCheck);
168 $httpResult = $httpClient->post(
177 if (!$licenseCheck && is_array($response) && isset($response[
'error']) && $response[
'error'] ===
'verification_needed')
179 return $this->
call($methodName, $additionalParams,
true);
185 'Strange answer from Bitrix Service! '
186 . static::SERVICE_URL
187 . static::SERVICE_PATH
189 . $httpClient->getStatus() .
' '
190 . implode(
" ", $httpClient->getError())
207 if(is_array($actions))
209 foreach($actions as $queryKey => $cmdData)
211 list($cmd, $cmdParams) = array_values($cmdData);
212 $batch[
'cmd'][$queryKey] = $cmd.(is_array($cmdParams) ?
'?'.http_build_query($this->
prepareRequestData($cmdParams)) :
'');
216 return $this->
call(static::METHOD_BATCH, $batch);
221 return $this->
call(static::METHOD_APPLICATION_ADD, array(
222 "TITLE" => $applicationSettings[
"TITLE"],
223 "REDIRECT_URI" => $applicationSettings[
"REDIRECT_URI"],
224 "SCOPE" => $applicationSettings[
"SCOPE"],
230 return $this->
call(static::METHOD_APPLICATION_UPDATE, array(
231 "CLIENT_ID" => $applicationSettings[
"CLIENT_ID"],
232 "TITLE" => $applicationSettings[
"TITLE"],
233 "REDIRECT_URI" => $applicationSettings[
"REDIRECT_URI"],
234 "SCOPE" => $applicationSettings[
"SCOPE"],
240 return $this->
call(static::METHOD_APPLICATION_DELETE, array(
241 "CLIENT_ID" => $applicationSettings[
"CLIENT_ID"],
247 $queryFields = array(
248 "CLIENT_ID" => $applicationSettings[
"CLIENT_ID"],
249 "VERSION" => $applicationSettings[
"VERSION"],
252 if(isset($applicationSettings[
"CHECK_HASH"]) && isset($applicationSettings[
"INSTALL_HASH"]))
254 $queryFields[
'CHECK_HASH'] = $applicationSettings[
"CHECK_HASH"];
255 $queryFields[
'INSTALL_HASH'] = $applicationSettings[
"INSTALL_HASH"];
258 if ($applicationSettings[
'BY_SUBSCRIPTION'] ===
'Y')
260 $method = static::METHOD_APPLICATION_INSTALL_SUBSCRIPTION;
264 $method = static::METHOD_APPLICATION_INSTALL;
267 return $this->
call($method, $queryFields);
272 return $this->
call(static::METHOD_APPLICATION_UNINSTALL, array(
273 "CLIENT_ID" => $applicationSettings[
"CLIENT_ID"],
279 return $this->
call(static::METHOD_REST_AUTHORIZE, array(
282 "PARAMS" => $additionalParams,
288 return $this->
call(static::METHOD_REST_CHECK, array(
289 "TOKEN" => $accessToken,
295 return $this->
call(static::METHOD_REST_CODE, array(
298 "PARAMS" => $additionalParams,
304 return $this->
call(static::METHOD_APPLICATION_LIST);
309 return $this->
call(static::METHOD_APPLICATION_USAGE, array(
316 return $this->
call(static::METHOD_REST_EVENT_CALL, array(
317 "QUERY" => $eventItems,
updateApplication(array $applicationSettings)
sendApplicationUsage(array $usage)
getRequestId(string $methodName)
installApplication(array $applicationSettings)
__construct($clientId, $clientSecret, $licenseKey)
getAuth($clientId, $scope, array $additionalParams=array())
const METHOD_APPLICATION_LIST
const METHOD_APPLICATION_ADD
prepareRequest($additionalParams, $licenseCheck=false)
const METHOD_REST_EVENT_CALL
const METHOD_APPLICATION_UNINSTALL
const METHOD_APPLICATION_STAT
const METHOD_APPLICATION_INSTALL
const HTTP_SOCKET_TIMEOUT
prepareRequestData($additionalParams)
call($methodName, $additionalParams=null, $licenseCheck=false)
const HTTP_STREAM_TIMEOUT
const METHOD_APPLICATION_USAGE
const METHOD_APPLICATION_INSTALL_SUBSCRIPTION
const METHOD_APPLICATION_VERSION_UPDATE
addApplication(array $applicationSettings)
getRequestUrl($methodName)
const METHOD_APPLICATION_VERSION_DELETE
getCode($clientId, $state, $additionalParams)
unInstallApplication(array $applicationSettings)
const METHOD_APPLICATION_DELETE
const METHOD_REST_AUTHORIZE
sendEvent(array $eventItems)
deleteApplication(array $applicationSettings)
const METHOD_APPLICATION_UPDATE