📜 ⬆️ ⬇️

We get i18n list of countries, regions, settlements from VKontakte

Get a database of countries, regions, localities with the translation of names and links for free and without registration? This is real!


When developing a small start-up project with big ambitions, I was faced with the need for a database of countries, regions and cities, translating names at least into the most common languages ​​(English, Russian, Spanish, German, etc.). A complete solution was not found, so I began to look for sources from which you can parse the necessary data. Well-known social networks immediately came to mind. This article has already described getting data from VKontakte, but having rummaged through the VKontakte API documentation, I was pleasantly surprised to find open documented methods for obtaining geodata:

database.getCountries - Returns a list of countries.

database.getRegions - Returns a list of regions.
')
database.getCities - Returns a list of locations.

database.getCountriesById - Returns information about countries by their identifiers

database.getCitiesById - Returns information about cities by their identifiers.

These are the main, interesting to us methods, besides not requiring authorization and use of tokens. More methods can be found in the documentation section .

An example url for accessing the method for obtaining a list of countries is presented below (the same is done using the https protocol):
http://api.vk.com/method/database.getCountries?v=5.5&need_all=1&count=10
View result
{"response":{"count":234,"items":[{"id":19,"title":""},{"id":20,"title":""},{"id":5,"title":""},{"id":21,"title":""},{"id":22,"title":""},{"id":23,"title":" "},{"id":24,"title":""},{"id":25,"title":""},{"id":26,"title":""},{"id":27,"title":" "}]}}



Options:
v - indicates the version of the API. In different versions of the JSON structure will be different. For example, starting with version 5.0, the count value has been added, which contains the total number of elements in the sample and, when used together with the offset parameter, will help get all the values. By default, the data is returned in the old version (apparently for compatibility with old applications).
count - the maximum number of values ​​returned by the method. The maximum value of the parameter that can be set is 1000 elements. The minimum and default values ​​for each method may vary and should be checked against the documentation.
need_all - optional, defaults to "0", indicates that small regions / countries / localities should not be sampled
It makes no sense to describe the remaining parameters for each function, as they are in the official documentation, let's move on to the unofficial possibilities:

We get a list of countries for different languages


 <?php $lang = 0; // russian $headerOptions = array( 'http' => array( 'method' => "GET", 'header' => "Accept-language: en\r\n" . "Cookie: remixlang=$lang\r\n" ) ); $methodUrl = 'http://api.vk.com/method/database.getCountries?v=5.5&need_all=1&count=1000'; $streamContext = stream_context_create($headerOptions); $json = file_get_contents($methodUrl, false, $streamContext); $arr = json_decode($json, true); echo 'Total countries count: ' . $arr['response']['count'] . ' loaded: ' . count($arr['response']['items']); print_r($arr['response']['items']); 


It should be noted that the method tries to return data in the language specified in the cookie parameter remixlang . Therefore, substituting the numerical value of the required language, we obtain data in the required language if there is a translation . Countries have translated everything, seemingly into all the languages ​​available on the social network. For regions and cities: if there is no translation (small settlements, regions of small countries), then we get the value in a language popular in this region, for example in African countries, in English, for CIS countries - Russian.

Here is a short list of language identifiers:
TongueRemixlang value
Russian0
Ukrainianone
English3
Spanishfour
Portuguese12
Deutsch6
Frenchsixteen
Italian7


Get a list of regions for different languages.


Example url to refer to the method of obtaining a list of regions for a country specified by the country_id parameter (a required parameter):
http://api.vk.com/method/database.getRegions?v=5.5&need_all=1&offset=0&count=1000&country_id=

 <?php $countryId = 1; // Russia $lang = 0; // russian $headerOptions = array( 'http' => array( 'method' => "GET", 'header' => "Accept-language: en\r\n" . //         "Cookie: remixlang=$lang\r\n" ) ); $methodUrl = 'http://api.vk.com/method/database.getRegions?v=5.5&need_all=1&offset=0&count=1000&country_id=' . $countryId; $streamContext = stream_context_create($headerOptions); $json = file_get_contents($methodUrl, false, $streamContext); $arr = json_decode($json, true); echo 'Total regions count: ' . $arr['response']['count'] . ' loaded: ' . count($arr['response']['items']); print_r($arr['response']['items']); 


The list of parameters for the method and description can be found in the documentation. However, it is worth noting that the returned JSON may be empty, that is, not all countries can be given regions , in this case, when getting a list of cities, the region_id parameter can be omitted.

We receive the list of settlements


Example url to refer to the method of obtaining a list of cities for the country specified by the country_id parameter (mandatory parameter) and region_id region (optional parameter):
http://api.vk.com/method/database.getCities?v=5.5&country_id=1®ion_id=1045244&offset=0&need_all=1&count=1000

 <?php $regionId = 1045244; $countryId = 1; // Russia $lang = 0; $headerOptions = array( 'http' => array( 'method' => "GET", 'header' => "Accept-language: en\r\n" . "Cookie: remixlang=$lang\r\n" ) ); $methodUrl = 'http://api.vk.com/method/database.getCities?v=5.5&country_id=' . $countryId . '®ion_id=' . $regionId . '&offset=0&need_all=1&count=1000'; $streamContext = stream_context_create($headerOptions); $json = file_get_contents($methodUrl, false, $streamContext); $arr = json_decode($json, true); echo 'Total cities count: ' . $arr['response']['count'] . ' loaded: ' . count($arr['response']['items']); print_r($arr['response']['items']); 


The returned data for each locality may also contain the following parameters:
area - the name of the area (province, etc.)
important equal "1" - designation of large cities, appears only when the need_all parameter is used

Attention! Some regions may not contain settlements (or themselves be settlements, as I understood).

Collisions


Some of the values ​​obtained may contain special characters, tags (for example, <br> ).

Updated
cookie, . uh_zuh , lang ( lang=ru).

Updated 2
, lang .

Updated 3
, :
GitHub

SQL PostgreSql
Yandex Disk
Google Drive
SQL MySql
Yandex Disk
Google Drive
CSV
- (;), , (")
Yandex Disk
Google Drive
- (,), , (")
Yandex Disk
Google Drive

: , i, , , , , , , , , , , ,
234
3 721
2 246 813
<br> ).

Updated
cookie, . uh_zuh , lang ( lang=ru).

Updated 2
, lang .

Updated 3
, :
GitHub

SQL PostgreSql
Yandex Disk
Google Drive
SQL MySql
Yandex Disk
Google Drive
CSV
- (;), , (")
Yandex Disk
Google Drive
- (,), , (")
Yandex Disk
Google Drive

: , i, , , , , , , , , , , ,
234
3 721
2 246 813

<br> ).

Updated
cookie, . uh_zuh , lang ( lang=ru).

Updated 2
, lang .

Updated 3
, :
GitHub

SQL PostgreSql
Yandex Disk
Google Drive
SQL MySql
Yandex Disk
Google Drive
CSV
- (;), , (")
Yandex Disk
Google Drive
- (,), , (")
Yandex Disk
Google Drive

: , i, , , , , , , , , , , ,
234
3 721
2 246 813

<br> ).

Updated
cookie, . uh_zuh , lang ( lang=ru).

Updated 2
, lang .

Updated 3
, :
GitHub

SQL PostgreSql
Yandex Disk
Google Drive
SQL MySql
Yandex Disk
Google Drive
CSV
- (;), , (")
Yandex Disk
Google Drive
- (,), , (")
Yandex Disk
Google Drive

: , i, , , , , , , , , , , ,
234
3 721
2 246 813

<br> ).

Updated
cookie, . uh_zuh , lang ( lang=ru).

Updated 2
, lang .

Updated 3
, :
GitHub

SQL PostgreSql
Yandex Disk
Google Drive
SQL MySql
Yandex Disk
Google Drive
CSV
- (;), , (")
Yandex Disk
Google Drive
- (,), , (")
Yandex Disk
Google Drive

: , i, , , , , , , , , , , ,
234
3 721
2 246 813

<br> ).

Updated
cookie, . uh_zuh , lang ( lang=ru).

Updated 2
, lang .

Updated 3
, :
GitHub

SQL PostgreSql
Yandex Disk
Google Drive
SQL MySql
Yandex Disk
Google Drive
CSV
- (;), , (")
Yandex Disk
Google Drive
- (,), , (")
Yandex Disk
Google Drive

: , i, , , , , , , , , , , ,
234
3 721
2 246 813

<br> ).

Updated
cookie, . uh_zuh , lang ( lang=ru).

Updated 2
, lang .

Updated 3
, :
GitHub

SQL PostgreSql
Yandex Disk
Google Drive
SQL MySql
Yandex Disk
Google Drive
CSV
- (;), , (")
Yandex Disk
Google Drive
- (,), , (")
Yandex Disk
Google Drive

: , i, , , , , , , , , , , ,
234
3 721
2 246 813

<br> ).

Updated
cookie, . uh_zuh , lang ( lang=ru).

Updated 2
, lang .

Updated 3
, :
GitHub

SQL PostgreSql
Yandex Disk
Google Drive
SQL MySql
Yandex Disk
Google Drive
CSV
- (;), , (")
Yandex Disk
Google Drive
- (,), , (")
Yandex Disk
Google Drive

: , i, , , , , , , , , , , ,
234
3 721
2 246 813

Source: https://habr.com/ru/post/204840/


All Articles