1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ExternalSender.php
См. документацию.
1<?php
2
3namespace Bitrix\MessageService\Providers\Edna\SMS;
4
5use Bitrix\Main\ArgumentException;
6use Bitrix\Main\Error;
7use Bitrix\Main\Localization\Loc;
8use Bitrix\Main\Text\StringHelper;
9use Bitrix\Main\Web\HttpClient;
10use Bitrix\Main\Web\Json;
11use Bitrix\MessageService\DTO;
12use Bitrix\MessageService\Sender\Result\HttpRequestResult;
13use Bitrix\MessageService\Sender\Util;
14
16{
17
18 protected string $apiKey;
19 protected string $apiEndpoint;
20
21 public function __construct(?string $apiKey, string $apiEndpoint, int $socketTimeout = 10, int $streamTimeout = 30)
22 {
23 $this->apiKey = $apiKey ?? '';
24 $this->apiEndpoint = $apiEndpoint;
25 $this->socketTimeout = $socketTimeout;
26 $this->streamTimeout = $streamTimeout;
27 }
28
29 public function callExternalMethod(string $method, ?array $requestParams = null, string $httpMethod = ''): HttpRequestResult
30 {
31 if ($this->apiKey === '')
32 {
34 $result->addError(new Error('Missing API key when requesting a service.'));
35
36 return $result;
37 }
38 $url = $this->apiEndpoint . $method;
39 $queryMethod = HttpClient::HTTP_GET;
40
41 $httpClient = new HttpClient([
42 'socketTimeout' => $this->socketTimeout,
43 'streamTimeout' => $this->streamTimeout,
44 'waitResponse' => true,
45 'version' => HttpClient::HTTP_1_1,
46 ]);
47 $httpClient->setHeader('User-Agent', static::USER_AGENT);
48 $httpClient->setHeader('Content-type', static::CONTENT_TYPE);
49 $httpClient->setHeader('X-API-KEY', $this->apiKey);
50 $httpClient->setCharset(static::CHARSET);
51
52 if (is_array($requestParams))
53 {
54 $queryMethod = HttpClient::HTTP_POST;
55 $requestParams = Json::encode($requestParams);
56 }
57
59 $result->setHttpRequest(new DTO\Request([
60 'method' => $queryMethod,
61 'uri' => $url,
62 'headers' => method_exists($httpClient, 'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
63 'body' => $requestParams
64 ]));
65
66 $answer = [];
67 $errorInfo = [];
68 if ($httpClient->query($queryMethod, $url, $requestParams))
69 {
70 $answer = $this->parseExternalAnswer($httpClient->getResult());
71
72 if ($httpClient->getStatus() !== 200)
73 {
74 $errorInfo = [
75 'code' => $httpClient->getStatus(),
76 'error' => $this->getMessageByErrorCode('error-' . $httpClient->getStatus()),
77 ];
78 }
79 }
80 else
81 {
82 $error = $httpClient->getError();
83 $errorInfo = [
84 'code' => key($error),
85 'error' => current($error),
86 ];
87 }
88 $result->setHttpResponse(new DTO\Response([
89 'statusCode' => $httpClient->getStatus(),
90 'headers' => $httpClient->getHeaders()->toArray(),
91 'body' => $httpClient->getResult(),
92 'error' => Util::getHttpClientErrorString($httpClient)
93 ]));
94
95 $result->setData($answer);
96
97 if (array_key_exists('code', $errorInfo) && $errorInfo['code'] !== 'ok')
98 {
99 $result->addError(new Error($errorInfo['error'], $errorInfo['code'], $errorInfo));
100 }
101
102 return $result;
103 }
104
105 protected function getMessageByErrorCode(string $code)
106 {
107 $locCode = 'MESSAGESERVICE_SENDER_SMS_SMSEDNARU_';
108 $locCode .= StringHelper::str_replace('-', '_', mb_strtoupper($code));
109
110 return Loc::getMessage($locCode) ?? $code;
111 }
112
113 protected function parseExternalAnswer(string $httpResult): array
114 {
115 try
116 {
117 return Json::decode($httpResult);
118 }
119 catch (ArgumentException $exception)
120 {
121 return ['error' => 'error-json-parsing'];
122 }
123 }
124}
Определения error.php:15
Определения request.php:10
Определения response.php:5
callExternalMethod(string $method, ?array $requestParams=null, string $httpMethod='')
Определения ExternalSender.php:29
__construct(?string $apiKey, string $apiEndpoint, int $socketTimeout=10, int $streamTimeout=30)
Определения ExternalSender.php:21
static getHttpClientErrorString(HttpClient $httpClient)
Определения util.php:24
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
$requestParams
Определения urlrewrite.php:46
$method
Определения index.php:27
$error
Определения subscription_card_product.php:20
$url
Определения iframe.php:7