1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
api.php
См. документацию.
1<?php
2
4
10
17final class Api
18{
22 private const HTTP_VERSION = HttpClient::HTTP_1_1;
23 private const HTTP_SOCKET_TIMEOUT = 10;
24 private const HTTP_STREAM_TIMEOUT = 10;
25
29 private const API_SEARCH_LIMIT = 10;
30
31 private const API_AUTOCOMPLETE_LIMIT = 7;
32
34 private $source;
35
40 public function __construct(OsmSource $source)
41 {
42 $this->source = $source;
43 }
44
45 public function search(array $options): array
46 {
47 $client = $this->makeHttpClient();
48
49 $body = $client->get(
50 $this->buildUrl(
51 'location',
52 'search',
53 $this->wrapQueryData(
54 [
55 'q' => $options['q'],
56 'addressdetails' => isset($options['addressdetails']) ? (int)$options['addressdetails'] : 0,
57 'limit' => isset($options['limit']) ? (int)$options['limit'] : self::API_SEARCH_LIMIT,
58 'accept-language' => $options['accept-language'] ?? '',
59 'format' => 'json',
60 ]
61 )
62 )
63 );
64
65 return $this->getResponse($client, $body);
66 }
67
68 public function autocomplete(array $options): array
69 {
70 $client = $this->makeHttpClient();
71
72 $queryData = [
73 'q' => $options['q'],
74 'limit' => isset($options['limit']) ? (int)$options['limit'] : self::API_AUTOCOMPLETE_LIMIT,
75 'lang' => $options['lang'] ?? '',
76 'version' => 2,
77 ];
78 if (isset($options['lat']) && isset($options['lon']))
79 {
80 $queryData['lat'] = $options['lat'];
81 $queryData['lon'] = $options['lon'];
82 }
83
84 $body = $client->get(
85 $this->buildUrl(
86 'autocomplete',
87 'autocomplete',
88 $this->wrapQueryData(
89 $queryData
90 )
91 )
92 );
93
94 return $this->getResponse($client, $body);
95 }
96
97 public function lookup(array $options): array
98 {
99 $client = $this->makeHttpClient();
100
101 $body = $client->get(
102 $this->buildUrl(
103 'location',
104 'lookup',
105 $this->wrapQueryData(
106 [
107 'osm_ids' => $options['osm_ids'] ?? '',
108 'addressdetails' => isset($options['addressdetails']) ? (int)$options['addressdetails'] : 0,
109 'accept-language' => $options['accept-language'] ?? '',
110 'format' => 'json',
111 ]
112 )
113 )
114 );
115
116 return $this->getResponse($client, $body);
117 }
118
119 public function details(array $options): array
120 {
121 $client = $this->makeHttpClient();
122
123 $body = $client->get(
124 $this->buildUrl(
125 'location',
126 'details',
127 $this->wrapQueryData(
128 [
129 'osmtype' => $options['osm_type'] ?? '',
130 'osmid' => $options['osm_id'] ?? '',
131 'format' => 'json',
132 'addressdetails' => isset($options['addressdetails']) ? (int)$options['addressdetails'] : 0,
133 'linkedplaces' => isset($options['linkedplaces']) ? (int)$options['linkedplaces'] : 0,
134 'hierarchy' => isset($options['hierarchy']) ? (int)$options['hierarchy'] : 0,
135 'accept-language' => $options['accept-language'] ?? '',
136 ]
137 )
138 )
139 );
140
141 return $this->getResponse($client, $body);
142 }
143
144 public function reverse(array $options): array
145 {
146 $client = $this->makeHttpClient();
147
148 $body = $client->get(
149 $this->buildUrl(
150 'location',
151 'reverse',
152 $this->wrapQueryData(
153 [
154 'lat' => isset($options['lat']) ? (float)$options['lat'] : null,
155 'lon' => isset($options['lng']) ? (float)$options['lng'] : null,
156 'zoom' => isset($options['zoom']) ? (int)$options['zoom'] : null,
157 'format' => 'json',
158 'addressdetails' => isset($options['addressdetails']) ? (int)$options['addressdetails'] : 0,
159 'accept-language' => $options['accept-language'] ?? '',
160 ]
161 )
162 )
163 );
164
165 return $this->getResponse($client, $body);
166 }
167
168 public function getStaticMap(
169 float $latitude,
170 float $longitude,
171 int $zoom,
172 int $width,
173 int $height,
174 ): array
175 {
176 $client = $this->makeHttpClient();
177
178 $body = $client->get(
179 $this->buildUrl(
180 'staticmap',
181 'get',
182 [
183 'latitude' => $latitude,
184 'longitude' => $longitude,
185 'zoom' => $zoom,
186 'width' => $width,
187 'height' => $height,
188 ],
189 )
190 );
191
192 $response = $this->getResponse($client, $body);
193
194 $response['data'] = isset($response['data']) ? base64_decode($response['data']) : null;
195
196 return $response;
197 }
198
199 private function getResponse(HttpClient $client, string $body): array
200 {
201 $status = $client->getStatus();
202
203 if ($body === false)
204 {
205 return [];
206 }
207
208 if ($status != 200)
209 {
210 return [];
211 }
212
213 try
214 {
215 $response = Json::decode($body);
216 }
217 catch (ArgumentException $e)
218 {
219 return [];
220 }
221
222 return is_array($response) ? $response : [];
223 }
224
225 private function makeHttpClient(): HttpClient
226 {
227 $token = $this->source->getOsmToken();
228
229 $result = new HttpClient(
230 [
231 'version' => self::HTTP_VERSION,
232 'socketTimeout' => self::HTTP_SOCKET_TIMEOUT,
233 'streamTimeout' => self::HTTP_STREAM_TIMEOUT,
234 ]
235 );
236
237 $result->setHeader(
238 'Authorization',
239 sprintf('Bearer %s', ($token ? $token->getToken() : ''))
240 );
241
242 $result->setHeader('Bx-Location-Osm-Host', $this->source->getOsmHostName());
243
244 return $result;
245 }
246
247 private function buildUrl(string $controller, string $action, array $queryData): string
248 {
249 $serviceUrl = $this->source->getOsmApiUrl();
250
251 if (!$serviceUrl)
252 {
253 throw new RuntimeException('Service url is not specified');
254 }
255
256 return sprintf(
257 '%s/?%s',
258 $serviceUrl,
259 http_build_query(
260 array_merge(
261 $queryData,
262 [
263 'action' => sprintf('osmgateway.%s.%s', $controller, $action)
264 ]
265 )
266 )
267 );
268 }
269
270 private function wrapQueryData(array $queryData): array
271 {
272 return [
273 'params' => $queryData
274 ];
275 }
276}
details(array $options)
Определения api.php:119
reverse(array $options)
Определения api.php:144
__construct(OsmSource $source)
Определения api.php:40
search(array $options)
Определения api.php:45
autocomplete(array $options)
Определения api.php:68
lookup(array $options)
Определения api.php:97
getStaticMap(float $latitude, float $longitude, int $zoom, int $width, int $height,)
Определения api.php:168
Определения json.php:9
$options
Определения commerceml2.php:49
</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
$status
Определения session.php:10
$width
Определения html.php:68
$response
Определения result.php:21
$action
Определения file_dialog.php:21