├── .gitignore ├── src ├── Exceptions │ └── YandexException.php └── Client.php ├── examples └── index.php ├── tests ├── Bootstrap.php └── ClientTest.php ├── composer.json ├── LICENSE ├── phpunit.xml.dist ├── README_RU.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | composer.phar 3 | /vendor/ 4 | composer.lock 5 | -------------------------------------------------------------------------------- /src/Exceptions/YandexException.php: -------------------------------------------------------------------------------- 1 | getScheduleOnStation('2200001111', Client::TRANSPORT_TYPE_TRAIN, Client::SYSTEM_EXPRESS); 11 | -------------------------------------------------------------------------------- /tests/Bootstrap.php: -------------------------------------------------------------------------------- 1 | =7.0", 23 | "guzzlehttp/guzzle": "~6.5.2" 24 | }, 25 | "require-dev": { 26 | "phpunit/phpunit": ">=6.5.5" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "sokolnikov911\\YandexSchedule\\": "src/" 31 | } 32 | }, 33 | "autoload-dev": { 34 | "psr-4": { 35 | "sokolnikov911\\YandexSchedule\\": "tests/" 36 | } 37 | }, 38 | "support": { 39 | "issues": "https://github.com/sokolnikov911/yandex-schedule/issues" 40 | } 41 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2017 by Petro Sokolnykov 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | tests 11 | 12 | 13 | 14 | 15 | 23 | 24 | 25 | 26 | 27 | src 28 | 29 | src 30 | 31 | 32 | 33 | vendor 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /README_RU.md: -------------------------------------------------------------------------------- 1 | PHP клиент API Яндекс расписаний 2 | ===================================== 3 | 4 | [![Latest Stable Version](https://poser.pugx.org/sokolnikov911/yandex-schedule/v/stable)](https://packagist.org/packages/sokolnikov911/yandex-schedule) 5 | [![Total Downloads](https://poser.pugx.org/sokolnikov911/yandex-schedule/downloads)](https://packagist.org/packages/sokolnikov911/yandex-schedule) 6 | [![Latest Unstable Version](https://poser.pugx.org/sokolnikov911/yandex-schedule/v/unstable)](https://packagist.org/packages/sokolnikov911/yandex-schedule) 7 | [![License](https://poser.pugx.org/sokolnikov911/yandex-schedule/license)](https://packagist.org/packages/sokolnikov911/yandex-schedule) 8 | [![composer.lock](https://poser.pugx.org/sokolnikov911/yandex-schedule/composerlock)](https://packagist.org/packages/sokolnikov911/yandex-schedule) 9 | 10 | 11 | PHP клиент API Яндекс расписаний. 12 | 13 | 14 | ## Примеры использования 15 | 16 | **Получние раcписания движения транспорта между двумя заданными станциями (например: между аэропортом Нью-Йорка и московским Шереметьево** 17 | 18 | ```php 19 | $client = new Client('yourApiKeyHere'); 20 | 21 | echo $client->getScheduleBetweenStations('NYC', 'SVO', 22 | Client::TRANSPORT_TYPE_PLANE, Client::SYSTEM_IATA); 23 | ``` 24 | 25 | 26 | **Получение раcписания для заданной станции (например: ж/д вокзал Киев-пассажирский)** 27 | 28 | ```php 29 | echo $client->getScheduleOnStation('2200001', Client::TRANSPORT_TYPE_TRAIN, Client::SYSTEM_EXPRESS); 30 | ``` 31 | 32 | 33 | **Получение списка станций для заданного маршрута (например: ж/д маршрут Бердянск - Киев)** 34 | 35 | ```php 36 | echo $client->getListStationsRoute('228P_1_2'); 37 | ``` 38 | 39 | 40 | **Получение информации о перевозчике (например: Turkish Airlines)** 41 | 42 | ```php 43 | echo $client->getCarrier('TK', Client::SYSTEM_IATA); 44 | ``` 45 | 46 | 47 | **Получение ближайших станций по заданным координатам** 48 | 49 | ```php 50 | echo $client->getNearestStations('50.440046', '40.4882367', '40'); 51 | ``` 52 | 53 | 54 | **Получение блока с копирайтом Яндекса** 55 | 56 | ```php 57 | echo $client->getCopyright(); 58 | ``` 59 | 60 | 61 | **Переключение между форматом загрузки данных (доступные форматы: XML и JSON ) и языковыми версяими (русский, украинский, турецкий)** 62 | 63 | По-умолчанию используется формат JSON и русский язык. 64 | 65 | ```php 66 | $client->setDataFormat(Client::DATA_FORMAT_XML); 67 | $client->setLanguage(Client::DATA_LANG_UK); 68 | ``` 69 | 70 | 71 | 72 | ## Установка 73 | 74 | Устанавливаем Composer 75 | 76 | ```bash 77 | curl -sS https://getcomposer.org/installer | php 78 | ``` 79 | 80 | Потом, запускаем команду композера для установки последней стабильной версии **yandex-schedule** 81 | 82 | ```bash 83 | php composer.phar require sokolnikov911/yandex-schedule 84 | ``` 85 | 86 | После установки подключаем автолоадер композера: 87 | 88 | ```php 89 | require 'vendor/autoload.php'; 90 | ``` 91 | 92 | Позже вы можете обновлять **yandex-schedule** используя композер: 93 | 94 | ```bash 95 | composer.phar update 96 | ``` 97 | 98 | 99 | ## Требования 100 | 101 | Этот API-клиент разработан для PHP7 (используется строгая типизация) и [Guzzle](https://github.com/guzzle/guzzle) 6. 102 | 103 | 104 | ## Лицензия 105 | 106 | This library is licensed under the MIT License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PHP Yandex Schedule (rasp) API client 2 | ===================================== 3 | 4 | [![Latest Stable Version](https://poser.pugx.org/sokolnikov911/yandex-schedule/v/stable)](https://packagist.org/packages/sokolnikov911/yandex-schedule) 5 | [![Total Downloads](https://poser.pugx.org/sokolnikov911/yandex-schedule/downloads)](https://packagist.org/packages/sokolnikov911/yandex-schedule) 6 | [![Latest Unstable Version](https://poser.pugx.org/sokolnikov911/yandex-schedule/v/unstable)](https://packagist.org/packages/sokolnikov911/yandex-schedule) 7 | [![License](https://poser.pugx.org/sokolnikov911/yandex-schedule/license)](https://packagist.org/packages/sokolnikov911/yandex-schedule) 8 | [![composer.lock](https://poser.pugx.org/sokolnikov911/yandex-schedule/composerlock)](https://packagist.org/packages/sokolnikov911/yandex-schedule) 9 | 10 | 11 | Russian version of README you can find here: [README_RU.md](https://github.com/sokolnikov911/yandex-schedule/blob/master/README_RU.md). 12 | 13 | Yandex Schedule (rasp) API client. 14 | 15 | 16 | ## Examples 17 | 18 | **Retrieving schedule between two stations (for example: New Your airport and Moscow Sheremetyevo)** 19 | 20 | ```php 21 | $client = new Client('yourApiKeyHere'); 22 | 23 | echo $client->getScheduleBetweenStations('NYC', 'SVO', 24 | Client::TRANSPORT_TYPE_PLANE, Client::SYSTEM_IATA); 25 | ``` 26 | 27 | 28 | **Retrieving schedule by station (for example: Kyiv-Passazhyrsky railway station)** 29 | 30 | ```php 31 | echo $client->getScheduleOnStation('2200001', Client::TRANSPORT_TYPE_TRAIN, Client::SYSTEM_EXPRESS); 32 | ``` 33 | 34 | **Retrieving stations list for selected route (for example: train Berdyansk - Kiev)** 35 | 36 | ```php 37 | echo $client->getListStationsRoute('228P_1_2'); 38 | ``` 39 | 40 | **Retrieving carrier information (for example: Turkish Airlines)** 41 | 42 | ```php 43 | echo $client->getCarrier('TK', Client::SYSTEM_IATA); 44 | ``` 45 | 46 | **Retrieving nearest stations** 47 | 48 | ```php 49 | echo $client->getNearestStations('50.440046', '40.4882367', '40'); 50 | ``` 51 | 52 | **Retrieving yandex copyright block** 53 | 54 | ```php 55 | echo $client->getCopyright(); 56 | ``` 57 | 58 | 59 | **Switching between data formats (XML and JSON available) and language versions (russian, ukrainian, turkish)** 60 | 61 | By default using JSON format and russian language. 62 | 63 | 64 | ```php 65 | $client->setDataFormat(Client::DATA_FORMAT_XML); 66 | $client->setLanguage(Client::DATA_LANG_UK); 67 | ``` 68 | 69 | 70 | 71 | ## Installing 72 | 73 | 74 | ```bash 75 | # Install Composer 76 | curl -sS https://getcomposer.org/installer | php 77 | ``` 78 | 79 | Next, run the Composer command to install the latest stable version of **yandex-schedule** 80 | 81 | ```bash 82 | php composer.phar require sokolnikov911/yandex-schedule 83 | ``` 84 | 85 | After installing, you need to require Composer's autoloader: 86 | 87 | ```php 88 | require 'vendor/autoload.php'; 89 | ``` 90 | 91 | You can then later update **yandex-schedule** using composer: 92 | 93 | ```bash 94 | composer.phar update 95 | ``` 96 | 97 | 98 | ## Requirements 99 | 100 | This client requires at least PHP7 (yeahh, type hinting!) and [Guzzle](https://github.com/guzzle/guzzle) 6. 101 | 102 | 103 | ## License 104 | 105 | This library is licensed under the MIT License. -------------------------------------------------------------------------------- /tests/ClientTest.php: -------------------------------------------------------------------------------- 1 | client = new Client(self::KEY); 17 | } 18 | 19 | public function testDefaultFormat() 20 | { 21 | $this->assertAttributeSame(Client::DATA_FORMAT_JSON, 'dataFormat', $this->client); 22 | } 23 | 24 | public function testSetDataFormatJSON() 25 | { 26 | $this->client->setDataFormat(Client::DATA_FORMAT_JSON); 27 | $this->assertAttributeSame(Client::DATA_FORMAT_JSON, 'dataFormat', $this->client); 28 | } 29 | 30 | public function testSetDataFormatXML() 31 | { 32 | $this->client->setDataFormat(Client::DATA_FORMAT_XML); 33 | $this->assertAttributeSame(Client::DATA_FORMAT_XML, 'dataFormat', $this->client); 34 | } 35 | 36 | public function testDefaultLanguage() 37 | { 38 | $this->assertAttributeSame(Client::DATA_LANG_RU, 'lang', $this->client); 39 | } 40 | 41 | public function testSetLanguage() 42 | { 43 | $this->client->setLanguage(Client::DATA_LANG_UK); 44 | $this->assertAttributeSame(Client::DATA_LANG_UK, 'lang', $this->client); 45 | } 46 | 47 | public function testGetScheduleBetweenStations() 48 | { 49 | $data = $this->client->getScheduleBetweenStations('NYC', 'LED', Client::TRANSPORT_TYPE_PLANE, Client::SYSTEM_IATA); 50 | $data = json_decode($data, true); 51 | 52 | $this->assertArrayHasKey('pagination', $data); 53 | $this->assertArrayHasKey('to', $data['search']); 54 | $this->assertArrayHasKey('from', $data['search']); 55 | $this->assertArrayHasKey('code', $data['search']['to']); 56 | $this->assertArrayHasKey('type', $data['search']['to']); 57 | $this->assertArrayHasKey('popular_title', $data['search']['to']); 58 | $this->assertArrayHasKey('short_title', $data['search']['to']); 59 | $this->assertArrayHasKey('title', $data['search']['to']); 60 | } 61 | 62 | public function testGetScheduleOnStation() 63 | { 64 | $data = $this->client->getScheduleOnStation('KBP', Client::TRANSPORT_TYPE_PLANE, Client::SYSTEM_IATA); 65 | $data = json_decode($data, true); 66 | 67 | $this->assertArrayHasKey('pagination', $data); 68 | $this->assertArrayHasKey('date', $data); 69 | $this->assertArrayHasKey('station', $data); 70 | $this->assertArrayHasKey('code', $data['station']); 71 | $this->assertArrayHasKey('schedule', $data); 72 | } 73 | 74 | public function testGetListStationsRoute() 75 | { 76 | $data = $this->client->getListStationsRoute('PS-773_0_c139_547'); 77 | $data = json_decode($data, true); 78 | 79 | $this->assertArrayHasKey('except_days', $data); 80 | $this->assertArrayHasKey('uid', $data); 81 | $this->assertArrayHasKey('title', $data); 82 | $this->assertArrayHasKey('start_time', $data); 83 | $this->assertArrayHasKey('number', $data); 84 | $this->assertArrayHasKey('short_title', $data); 85 | $this->assertArrayHasKey('days', $data); 86 | $this->assertArrayHasKey('carrier', $data); 87 | $this->assertArrayHasKey('stops', $data); 88 | $this->assertArrayHasKey('transport_type', $data); 89 | $this->assertArrayHasKey('code', $data['carrier']); 90 | $this->assertArrayHasKey('codes', $data['carrier']); 91 | $this->assertArrayHasKey('title', $data['carrier']); 92 | } 93 | 94 | public function testGetCarrier() 95 | { 96 | $data = $this->client->getCarrier('TK', Client::SYSTEM_IATA); 97 | $data = json_decode($data, true); 98 | 99 | $this->assertArrayHasKey('carriers', $data); 100 | $this->assertArrayHasKey('code', $data['carriers'][0]); 101 | $this->assertArrayHasKey('title', $data['carriers'][0]); 102 | $this->assertArrayHasKey('url', $data['carriers'][0]); 103 | $this->assertArrayHasKey('contacts', $data['carriers'][0]); 104 | $this->assertArrayHasKey('codes', $data['carriers'][0]); 105 | $this->assertArrayHasKey('address', $data['carriers'][0]); 106 | $this->assertArrayHasKey('logo', $data['carriers'][0]); 107 | $this->assertArrayHasKey('email', $data['carriers'][0]); 108 | } 109 | 110 | public function testGetNearestStations() 111 | { 112 | $data = $this->client->getNearestStations('50.440046', '40.4882367', '40'); 113 | $data = json_decode($data, true); 114 | 115 | $this->assertArrayHasKey('pagination', $data); 116 | $this->assertArrayHasKey('stations', $data); 117 | $this->assertArrayHasKey('distance', $data['stations'][0]); 118 | $this->assertArrayHasKey('code', $data['stations'][0]); 119 | $this->assertArrayHasKey('title', $data['stations'][0]); 120 | $this->assertArrayHasKey('station_type', $data['stations'][0]); 121 | $this->assertArrayHasKey('popular_title', $data['stations'][0]); 122 | $this->assertArrayHasKey('short_title', $data['stations'][0]); 123 | $this->assertArrayHasKey('transport_type', $data['stations'][0]); 124 | $this->assertArrayHasKey('lat', $data['stations'][0]); 125 | $this->assertArrayHasKey('lng', $data['stations'][0]); 126 | $this->assertArrayHasKey('type', $data['stations'][0]); 127 | } 128 | 129 | public function testGetCopyright() 130 | { 131 | $data = $this->client->getCopyright(); 132 | $data = json_decode($data, true); 133 | 134 | $this->assertArrayHasKey('copyright', $data); 135 | $this->assertArrayHasKey('logo_vm', $data['copyright']); 136 | $this->assertArrayHasKey('logo_hd', $data['copyright']); 137 | $this->assertArrayHasKey('logo_vy', $data['copyright']); 138 | $this->assertArrayHasKey('logo_vd', $data['copyright']); 139 | $this->assertArrayHasKey('logo_hm', $data['copyright']); 140 | $this->assertArrayHasKey('logo_hy', $data['copyright']); 141 | $this->assertArrayHasKey('url', $data['copyright']); 142 | $this->assertArrayHasKey('text', $data['copyright']); 143 | } 144 | 145 | public function testGetCopyrightXML() 146 | { 147 | $this->client->setDataFormat(Client::DATA_FORMAT_XML); 148 | $data = $this->client->getCopyright(); 149 | 150 | $p = xml_parser_create(); 151 | xml_parse_into_struct($p, $data, $array); 152 | xml_parser_free($p); 153 | 154 | $this->assertTrue(array_search('COPYRIGHT', array_column($array, 'tag')) ? true : false); 155 | $this->assertTrue(array_search('LOGO_VY', array_column($array, 'tag')) ? true : false); 156 | } 157 | 158 | public function testGetApiVersion() 159 | { 160 | $data = $this->client->getApiVersion(); 161 | 162 | $this->assertEquals('v1.0', $data); 163 | } 164 | } -------------------------------------------------------------------------------- /src/Client.php: -------------------------------------------------------------------------------- 1 | key = $key; 58 | } 59 | 60 | /** 61 | * Sets format of response data 62 | * 63 | * @param string $format json|xml 64 | */ 65 | public function setDataFormat(string $format) 66 | { 67 | $this->dataFormat = $format; 68 | } 69 | 70 | /** 71 | * Sets language of response data 72 | * 73 | * @param string $lang Language of data. 74 | */ 75 | public function setLanguage(string $lang) 76 | { 77 | $this->lang = $lang; 78 | } 79 | 80 | /** 81 | * @param string $from Departure station code, for example NYC (for New York airport) 82 | * @param string $to Arrival station code, for example LED (for Saint Petersburg airport Pulkovo) 83 | * @param string $date Date to which you want to receive a list of flights. Should be specified in the format "YYYY-MM-DD". By default, the list of flights for all dates will be returned. 84 | * @param string $transportTypes Transport type 85 | * @param string $system 86 | * @param int $offset Offset of data. 87 | * @param string $showSystems System Code for answer 88 | * 89 | * @see https://tech.yandex.ru/rasp/doc/reference/schedule-point-point-docpage/ 90 | * 91 | * @throws YandexException 92 | * @throws ClientException 93 | * 94 | * @return string Data 95 | */ 96 | public function getScheduleBetweenStations(string $from, string $to, string $transportTypes, string $system, 97 | string $date = '', int $offset = 0, string $showSystems = '') 98 | { 99 | $queryArray = [ 100 | 'from' => $from, 101 | 'to' => $to, 102 | 'date' => $date, 103 | 'transport_types' => $transportTypes, 104 | 'system' => $system, 105 | 'offset' => $offset, 106 | 'show_systems' => $showSystems 107 | ]; 108 | 109 | return $this->getData($this->getEndpointUrl(self::ENDPOINT_SEARCH, $queryArray)); 110 | } 111 | 112 | /** 113 | * @param string $station Departure station code, for example NYC (for New York airport) 114 | * @param string $date Date to which you want to receive a list of flights. Should be specified in the format "YYYY-MM-DD". By default, the list of flights for all dates will be returned. 115 | * @param string $transportTypes Transport type 116 | * @param string $system 117 | * @param string $event Event of schedule 118 | * @param string $showSystems System Code for answer 119 | * @param string $direction 120 | * @param int $offset Offset of data 121 | * 122 | * @see https://tech.yandex.ru/rasp/doc/reference/schedule-on-station-docpage/ 123 | * 124 | * @throws YandexException 125 | * @throws ClientException 126 | * 127 | * @return string Data 128 | */ 129 | public function getScheduleOnStation(string $station, string $transportTypes, string $system, 130 | string $event = '', string $direction = '', string $showSystems = '', 131 | string $date = '', int $offset = 0) 132 | { 133 | $queryArray = [ 134 | 'station' => $station, 135 | 'event' => $event, 136 | 'date' => $date, 137 | 'transport_types' => $transportTypes, 138 | 'system' => $system, 139 | 'show_systems' => $showSystems, 140 | 'direction' => $direction, 141 | 'offset' => $offset 142 | ]; 143 | 144 | return $this->getData($this->getEndpointUrl(self::ENDPOINT_SCHEDULE, $queryArray)); 145 | } 146 | 147 | /** 148 | * @param string $uid 149 | * @param string $showSystems System Code for answer 150 | * @param string $date Date to which you want to receive a list of flights. Should be specified in the format "YYYY-MM-DD". By default, the list of flights for all dates will be returned. 151 | * 152 | * @see https://tech.yandex.ru/rasp/doc/reference/list-stations-route-docpage/ 153 | * 154 | * @throws YandexException 155 | * @throws ClientException 156 | * 157 | * @return string Data 158 | */ 159 | public function getListStationsRoute(string $uid, string $showSystems = '', string $date = '') 160 | { 161 | $queryArray = [ 162 | 'uid' => $uid, 163 | 'date' => $date, 164 | 'show_systems' => $showSystems, 165 | ]; 166 | 167 | return $this->getData($this->getEndpointUrl(self::ENDPOINT_THREAD, $queryArray)); 168 | } 169 | 170 | /** 171 | * @param string $code Code of carrier 172 | * @param string $system 173 | * 174 | * @see https://tech.yandex.ru/rasp/doc/reference/query-carrier-docpage/ 175 | * 176 | * @throws YandexException 177 | * @throws ClientException 178 | * 179 | * @return string Data 180 | */ 181 | public function getCarrier(string $code, string $system = '') 182 | { 183 | $queryArray = [ 184 | 'code' => $code, 185 | 'system' => $system 186 | ]; 187 | 188 | return $this->getData($this->getEndpointUrl(self::ENDPOINT_CARRIER, $queryArray)); 189 | } 190 | 191 | /** 192 | * @param string $lat Latitude according to WGS84 193 | * @param string $lng Longitude according to WGS84 194 | * @param integer $distance Radius in km (from 0 to 50). 195 | * @param string $stationType 196 | * @param string $transportTypes Transport type 197 | * @param int $offset Offset of data. 198 | * 199 | * @see https://tech.yandex.ru/rasp/doc/reference/query-nearest-station-docpage/ 200 | * 201 | * @throws YandexException 202 | * @throws ClientException 203 | * 204 | * @return string Data 205 | */ 206 | public function getNearestStations(string $lat, string $lng, int $distance, 207 | string $stationType = '', string $transportTypes = '', 208 | int $offset = 0) 209 | { 210 | $queryArray = [ 211 | 'lat' => $lat, 212 | 'lng' => $lng, 213 | 'distance' => $distance, 214 | 'station_type' => $stationType, 215 | 'transport_types' => $transportTypes, 216 | 'offset' => $offset 217 | ]; 218 | 219 | return $this->getData($this->getEndpointUrl(self::ENDPOINT_NEAREST_STATIONS, $queryArray)); 220 | } 221 | 222 | /** 223 | * @see https://tech.yandex.ru/rasp/doc/reference/query-copyright-docpage/ 224 | * 225 | * @throws YandexException 226 | * @throws ClientException 227 | * 228 | * @return string Data 229 | */ 230 | public function getCopyright() 231 | { 232 | return $this->getData($this->getEndpointUrl(self::ENDPOINT_COPYRIGHT)); 233 | } 234 | 235 | /** 236 | * @return string Used API version 237 | */ 238 | public function getApiVersion(): string 239 | { 240 | return $this->apiVersion; 241 | } 242 | 243 | /** 244 | * Sends a request 245 | * 246 | * @param string $url Full URL of end-point 247 | * 248 | * @throws YandexException 249 | * @throws ClientException 250 | * 251 | * @return string Response body 252 | */ 253 | protected function getData(string $url): string 254 | { 255 | $client = new HttpClient(); 256 | 257 | try { 258 | $response = $client->get($url); 259 | } catch (ClientException $e) { 260 | $response = $e->getResponse(); 261 | $responseData = $response->getBody()->getContents(); 262 | 263 | if ($this->dataFormat == self::DATA_FORMAT_XML) { 264 | $xml = simplexml_load_string($responseData); 265 | $responseData = json_encode($xml, JSON_UNESCAPED_UNICODE); 266 | } 267 | 268 | $dataArray = json_decode($responseData, true); 269 | 270 | if ($dataArray['error'] && $dataArray['error']['text']) { 271 | throw new YandexException($dataArray['error']['text']); 272 | } else throw $e; 273 | } 274 | 275 | return $response->getBody(); 276 | } 277 | 278 | /** 279 | * Sends a request 280 | * 281 | * @param string $type Type of end-point 282 | * @param array $dataArray Additional data 283 | * 284 | * @return string Full end-point URL 285 | */ 286 | protected function getEndpointUrl(string $type, array $dataArray = []): string 287 | { 288 | $settingsArray = [ 289 | 'lang' => $this->lang, 290 | 'format' => $this->dataFormat, 291 | 'apikey' => $this->key 292 | ]; 293 | 294 | return $this->apiUrl . $this->apiVersion . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . 295 | '?' . http_build_query(array_merge($settingsArray, $dataArray)); 296 | } 297 | } --------------------------------------------------------------------------------