- Модуль: imbot
- Путь к файлу: ~/bitrix/modules/imbot/lib/http.php
- Класс: BitrixImBotHttp
- Вызов: Http::query
public function query($command, $params = array(), $waitResponse = false)
{
if ($command == '' || !is_array($params) || !$this->botId)
{
return false;
}
foreach ($params as $key => $value)
{
$value = $value === '0' ? '#ZERO#' : $value;
$params[$key] = empty($value) ? '#EMPTY#' : $value;
}
$params['BX_COMMAND'] = $command;
$params['BX_BOT'] = $this->botId;
$params['BX_LICENCE'] = $this->licenceCode;
$params['BX_DOMAIN'] = $this->domain;
$params['BX_TYPE'] = $this->type;
$params['BX_REGION'] = $this->region;
$params['BX_VERSION'] = MainModuleManager::getVersion('imbot');
$params['BX_LANG'] = BitrixMainLocalizationLoc::getCurrentLang();
$params = BitrixMainTextEncoding::convertEncoding($params, SITE_CHARSET, 'UTF-8');
$params['BX_HASH'] = self::requestSign($this->type, $params);
$waitResponse = $waitResponse ? true : (bool)Option::get('imbot', 'wait_response', false);
Log::write([$this->controllerUrl, $params], 'COMMAND: '.$command);
$controllerUrl = $this->controllerUrl.'?';
$controllerUrl .= 'BOT='.$this->botId.'&';
$controllerUrl .= 'COMMAND='.$command;
$httpClient = $this->instanceHttpClient($waitResponse);
$result = $httpClient->post($controllerUrl, $params);
$errorCode = $httpClient->getHeaders()->get('x-bitrix-error');
Log::write(['response' => $result, 'error' => $errorCode], 'COMMAND RESULT: '.$command);
if ($result === false)
{
// check for network errors
$errors = $httpClient->getError();
if (!empty($errors))
{
$result = [
'error' => [
'code' => self::ERROR_NETWORK,
'msg' => 'Network connection error.',
'errorStack' => $errors,
]
];
}
}
elseif ($waitResponse)
{
// try to parse result
if (is_string($result))
{
try
{
$result = Json::decode($result);
}
catch (ArgumentException $exception)
{
$result = [
'error' => [
'code' => self::ERROR_ANSWER,
'msg' => 'Server answer is malformed.',
'errorResult' => $result,
'errorStack' => [
$exception->getCode() => $exception->getMessage()
],
]
];
}
}
}
// don't wait for response body
else
{
$result = ($result !== false);
if ($errorCode)
{
$result = [
'error' => [
'code' => $errorCode,
]
];
}
}
return $result;
}