- Модуль: sale
- Путь к файлу: ~/bitrix/modules/sale/lib/services/base/restclient.php
- Класс: BitrixSaleServicesBaseRestClient
- Вызов: RestClient::call
protected function call($methodName, $additionalParams = null, $licenseCheck = false, $clearAccessSettings = false)
{
$result = new ResultSerializable();
if(!self::isServerAlive() && !defined('SALE_SRVS_RESTCLIENT_DISABLE_SRV_ALIVE_CHECK'))
{
$result->addError(
new Error(
Loc::getMessage('SALE_SRV_BASE_REST_CONNECT_ERROR').' '.$this->getServiceHost(),
self::ERROR_SERVICE_UNAVAILABLE
)
);
return $result;
}
if ($clearAccessSettings)
{
$this->clearAccessSettings();
$this->accessSettings = null;
}
if (is_null($this->accessSettings))
{
$this->accessSettings = $this->getAccessSettings();
}
if (!$this->accessSettings)
{
$result->addError(new Error(Loc::getMessage('SALE_SRV_BASE_REST_ACCESS_SETTINGS_ERROR')));
return $result;
}
if (!is_array($additionalParams))
$additionalParams = array();
else
$additionalParams = Encoding::convertEncodingArray($additionalParams, LANG_CHARSET, "utf-8");
$additionalParams['version'] = $this->version;
$additionalParams['client_id'] = $this->accessSettings['client_id'];
$additionalParams['client_secret'] = $this->accessSettings['client_secret'];
$additionalParams['lang'] = LANGUAGE_ID;
if ($licenseCheck)
{
$additionalParams = static::signLicenseRequest($additionalParams, static::getLicense());
}
$host = $this->getServiceHost();
$http = new HttpClient([
'socketTimeout' => $this->httpTimeout,
'streamTimeout' => $this->streamTimeout,
]);
$postResult = @$http->post(
$host.static::REST_URI.$methodName,
$additionalParams
);
try
{
$answer = $this->prepareAnswer($postResult);
}
catch(Exception $e)
{
$answer = false;
}
if (!is_array($answer))
{
$result->addError(new Error(Loc::getMessage('SALE_SRV_BASE_REST_ANSWER_ERROR').' '.$this->getServiceHost().'. (Status: "'.$http->getStatus().'", Result: "'.$postResult.'")', static::ERROR_SERVICE_UNAVAILABLE));
$this->setLastUnSuccessCallInfo();
return $result;
}
if(self::getLastUnSuccessCount() > 0)
$this->setLastUnSuccessCallInfo(true);
if (array_key_exists('error', $answer))
{
if ($answer['error'] === 'verification_needed')
{
if($licenseCheck)
{
$result->addError(new Error($answer['error'].". ".$answer['error_description'], self::ERROR_WRONG_LICENSE));
return $result;
}
else
{
return $this->call($methodName, $additionalParams, true);
}
}
else if (($answer['error'] === 'ACCESS_DENIED' || $answer['error'] === 'Invalid client' || $answer['error'] === 'NO_AUTH_FOUND')
&& !$clearAccessSettings)
{
return $this->call($methodName, $additionalParams, true, true);
}
$result->addError(new Error($answer['error'].". ".$answer['error_description']));
return $result;
}
if ($answer['result'] == false)
$result->addError(new Error('Nothing found', static::ERROR_NOTHING_FOUND));
if (is_array($answer['result']))
$result->addData($answer['result']);
return $result;
}