75 lines
2.4 KiB
PHP
75 lines
2.4 KiB
PHP
|
<?php
|
|||
|
|
|||
|
/* ----------------------------------------------------------------------
|
|||
|
14.10.2022
|
|||
|
Смотрим, есть ли в БД регион и если нет - добавляем
|
|||
|
---------------------------------------------------------------------- */
|
|||
|
|
|||
|
function add_geo($db, $res){
|
|||
|
$a['txt']=$res['country'];
|
|||
|
$strana_id=$db->get_val('strana', $a, 'id');
|
|||
|
if (!$strana_id) $strana_id = $db->add('strana', $a);
|
|||
|
|
|||
|
unset($a);
|
|||
|
$a['txt']=$res['regionName'];
|
|||
|
$a['strana_id']=$strana_id;
|
|||
|
$region_id=$db->get_val('region', $a, 'id');
|
|||
|
if (!$region_id) $region_id = $db->add('region', $a);
|
|||
|
$_SESSION['region']=$region_id;
|
|||
|
|
|||
|
unset($a);
|
|||
|
$a['txt']=$res['city'];
|
|||
|
$a['region_id']=$region_id;
|
|||
|
$a['strana_id']=$strana_id;
|
|||
|
$city_id=$db->get_val('city', $a, 'id');
|
|||
|
if (!$city_id)$sity_id=$db->add('city', $a);
|
|||
|
$_SESSION['city']=$sity_id;
|
|||
|
}
|
|||
|
|
|||
|
/* ----------------------------------------------------------------------
|
|||
|
15.10.2022
|
|||
|
С помощью стороннего сервиса получаем данные о местоположении
|
|||
|
---------------------------------------------------------------------- */
|
|||
|
|
|||
|
function get_geo_api($ip){
|
|||
|
$ch = curl_init('http://ip-api.com/json/' . $ip . '?lang=ru');
|
|||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|||
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|||
|
$res = curl_exec($ch);
|
|||
|
curl_close($ch);
|
|||
|
$res = json_decode($res, true);
|
|||
|
return $res;
|
|||
|
}
|
|||
|
|
|||
|
$db->free_sql2('CREATE TABLE IF NOT EXISTS `strana` (
|
|||
|
`id` INTEGER PRIMARY KEY NOT NULL,
|
|||
|
`txt` TEXT )');
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
$db->free_sql2('CREATE TABLE IF NOT EXISTS `city` (
|
|||
|
`id` INTEGER PRIMARY KEY NOT NULL,
|
|||
|
`txt` TEXT,
|
|||
|
`region_id` INTEGER,
|
|||
|
`strana_id` INTEGER,
|
|||
|
FOREIGN KEY (`region_id`) REFERENCES `region` (`id`),
|
|||
|
FOREIGN KEY (`strana_id`) REFERENCES `strana` (`id`)
|
|||
|
)');
|
|||
|
|
|||
|
$db->free_sql2('CREATE TABLE IF NOT EXISTS `street` (
|
|||
|
`id` INTEGER PRIMARY KEY NOT NULL,
|
|||
|
`txt` TEXT,
|
|||
|
`city_id` INTEGER,
|
|||
|
FOREIGN KEY (`city_id`) REFERENCES `city` (`id`))');
|
|||
|
|
|||
|
|
|||
|
/* ----------------------------------------------------------------------
|
|||
|
14.10.2022
|
|||
|
Создаем базу городов и регионов. Надо сделать асинхронно
|
|||
|
---------------------------------------------------------------------- */
|
|||
|
|
|||
|
add_geo( $db, get_geo_api ( $_SERVER['REMOTE_ADDR'] ) );
|
|||
|
|
|||
|
?>
|