• Модуль: location
  • Путь к файлу: ~/bitrix/modules/location/lib/source/google/requesters/baserequester.php
  • Класс: BitrixLocationSourceGoogleRequestersBase
  • Вызов: Base::request
public function request(array $params)
{
	$url = $this->makeUrl($params);
	$result = false;
	$httpRes = false;
	$loggerService = $this->getLoggerService();
	$loggerService->log(LogLevel::DEBUG, $url, EventType::SOURCE_GOOGLE_REQUESTER_URL);
	$cacheItemKey = $this->createCacheItemKey($url);
	$takenFromCache = false;

	if($this->cachePool && $cachedAnswer = $this->cachePool->getItem($cacheItemKey))
	{
		$httpRes = $cachedAnswer['httpRes'];
		$errors = $cachedAnswer['errors'];
		$status = $cachedAnswer['status'];

		$takenFromCache = true;
	}
	else
	{
		if (@$this->httpClient->get($url))
		{
			$httpRes = $this->httpClient->getResult();
		}

		$errors = $this->httpClient->getError();
		$status = $this->httpClient->getStatus();
	}

	$loggerService->log(
		LogLevel::DEBUG,
		'{"httpRes":"'.$httpRes.'","errors":"'.	$this->convertErrorsToString($errors).'","status":"'.$status.'"}',
		EventType::SOURCE_GOOGLE_REQUESTER_RESULT
	);

	if(!$httpRes && !empty($errors))
	{
		throw new RuntimeException(
			$this->convertErrorsToString($errors),
			ErrorCodes::REQUESTER_BASE_HTTP_ERROR
		);
	}
	else
	{
		$result = [];

		if($httpRes)
		{
			try
			{
				$result = Json::decode($httpRes);
			}
			catch(Exception $e)
			{
				$message = 'Can't decode Google server's answer.'
					. ' URL: ' . $url
					. ' Answer: '. $httpRes;

				throw new RuntimeException($message, ErrorCodes::REQUESTER_BASE_JSON_ERROR);
			}

			if(!$takenFromCache
				&& $this->cachePool
				&& $status === 200
				&& empty($errors)
				&& isset($result['status'])
				&& $result['status'] === 'OK'
			)
			{
				$this->cachePool->addItem(
					$cacheItemKey,
					[
						'httpRes' => $httpRes,
						'errors' => $errors,
						'status' => $status
					]
				);
			}
		}

		if ($status != 200)
		{
			$message = 'Http status: '.$status
				. ' URL: ' . $url
				. ' Answer: '. $httpRes;

			throw new RuntimeException($message, ErrorCodes::REQUESTER_BASE_STATUS_ERROR);
		}
	}

	return $result;
}