Please tell me how I can integrate the List of cities and offices from this site into my website in ordering. Json format, I have no idea how to do it all. For any tips I will be very grateful !!!
- Created an API key, pasted where requested. But then I don’t know how to integrate the selection of cities and departments on my site! - Coffee Kyiv
- It is necessary to keep a list of cities and offices in your database. You can get a list of Curl in PHP, recently did, I can help - AndreyVitovtov
- I would be very grateful for the help. I want to take this spsmok of cities and offices from the transport service through API - Coffee Kyiv
|
1 answer
/*CONNECT DATABASE*/ $mysqli = new mysqli("/*Адрес*/", "/*login*/", "/*password*/", "/*Имя базы данных*/"); if($mysqli->connect_errno) { echo "Не удалось подключиться к MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; exit(); } if (!$mysqli->set_charset("utf8")) { printf("Ошибка при загрузке набора символов utf8: %s\n", $mysqli->error); exit(); } /*END CONNECT DATABASE*/ /*ОТПРАВКА ЗАПРОСА*/ function sendRequestNP($application) { $url = "https://api.novaposhta.ua/v2.0/json/"; $application = json_encode($application); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $application); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($application)) ); $response = curl_exec($ch); curl_close($ch); return $response = json_decode($response, true); } /*СПРАВОЧНИК ГОРОДОВ КОМПАНИИ*/ function locality($KeyAPI) { $application = array( "apiKey" => $KeyAPI, "modelName" => "Address", "calledMethod" => "getCities" ); return sendRequestNP($application); } /*СПРАВОЧНИК ОТДЕЛЕНИЙ И ТИПОВ ОТДЕЛЕНИЙ*/ function offices($KeyAPI) { $application = array( "apiKey" => $KeyAPI, "modelName" => "Address", "calledMethod" => "getWarehouses", "methodProperties" => array( "Language" => "ua" ) ); return sendRequestNP($application); } $KeyAPI = "/*API ключ*/"; $response = locality($KeyAPI); if($response[success] == '1') { /*Очистить Таблицу*/ $truncate = "TRUNCATE TABLE NovaPoshta_cities"; $mysqli->query($truncate); foreach($response[data] as $city) { /*Добавить в таблицу новые записи*/ $insert = "INSERT INTO NovaPoshta_cities (Description, DescriptionRu, Ref, Area, SettlementType, SettlementTypeDescription, SettlementTypeDescriptionRu) VALUES ('".$city[Description]."', '".$city[DescriptionRu]."', '".$city[Ref]."', '".$city[Area]."', '".$city[SettlementType]."', '".$city[SettlementTypeDescription]."', '".$city[SettlementTypeDescriptionRu]."')"; $mysqli->query($insert); } } - I'll check how it works and accomplish your goal !! Thank! - Coffee Kyiv
- These functions on the checkout page add? How to add a list of cities to the database? It is possible specifically what and why? I don't understand well)) - Coffee Kyiv
- one). In the database, create a table for example "NovaPoshta_cities" 2). Create a file with any name that you want to run on a schedule once a day. 3). Insert all the code above into the created file - AndreyVitovtov
- In the table rows Description, DescriptionRu, Ref, Area, SettlementType, SettlementTypeDescription, SettlementTypeDescriptionRu - AndreyVitovtov
- So add to the database "Cities of the company", "branches" as well. If it is not clear write, I will answer tomorrow. - AndreyVitovtov
|