Browse Source

feat: accurate city address in China

master
Dnomd343 4 years ago
parent
commit
91d17e7dd8
  1. BIN
      backend/ChinaCity.db
  2. 44
      backend/city.php
  3. 26
      backend/queryInfo.php

BIN
backend/ChinaCity.db

Binary file not shown.

44
backend/city.php

@ -0,0 +1,44 @@
<?php
// 数据来源:ChinaCity.db
// 请求方式:getLoc($region, $city)
// 返回格式:
// {
// "region": 省/自治区/直辖市名称
// "city": 市/自治州名称
// "lat": 地区纬度
// "lon": 地区经度
// }
class cityDB extends SQLite3 {
function __construct() {
$this->open('ChinaCity.db'); // 中国省市经纬度数据库
}
}
function getLoc($region, $city) { // 根据省份/城市信息查询经纬度
$db = new cityDB;
$data['region'] = $region;
$data['city'] = $city;
$query_str='SELECT * FROM main WHERE level1="'.$region.'" AND level2="'.$city.'";';
$raw = $db->query($query_str)->fetchArray(SQLITE3_ASSOC);
if (!$raw) { // 查无数据
$query_str='SELECT * FROM main WHERE level1="'.$region.'" AND level2="-";'; // 尝试仅查询省份数据
$raw = $db->query($query_str)->fetchArray(SQLITE3_ASSOC);
if (!$raw) { // 省份错误,返回北京经纬度
$data['lat'] = '39.91';
$data['lon'] = '116.73';
return $data;
}
if ($city == '') {
$query_str='SELECT * FROM main WHERE level1="'.$region.'" LIMIT 1,1;'; // 获取省会记录
$raw = $db->query($query_str)->fetchArray(SQLITE3_ASSOC);
$data['city'] = $raw['level2'];
}
}
$data['lat'] = $raw['lat'];
$data['lon'] = $raw['lon'];
return $data;
}
?>

26
backend/queryInfo.php

@ -1,14 +1,12 @@
<?php
// include("getInfo.php");
include("country.php");
include("qqwry.php");
include("ipinfo.php");
include("ipip.php");
include("city.php");
function getIPInfo($ip) {
$qqwry = new QQWry('qqwry.dat');
$detail = $qqwry->getDetail($ip);
$specialInfo = checkSpecial($ip);
if (is_string($specialInfo)) {
$info['ip'] = $ip;
@ -20,9 +18,9 @@ function getIPInfo($ip) {
$info['loc'] = null;
$info['isp'] = $specialInfo;
} else {
$data = IPinfo::getInfo($ip);
$IPIP = new IPDB('ipipfree.ipdb');
$addr = $IPIP->getDistrict($ip);
$data = IPinfo::getInfo($ip);
$country = getCountry($data['country']);
$info['ip'] = $data['ip'];
$info['as'] = $data['as'];
@ -36,14 +34,24 @@ function getIPInfo($ip) {
if ($addr[0] == '中国') {
$info['country'] = 'CN - China(中国)';
$info['timezone'] = 'Asia/Shanghai';
if ($addr[1] && $addr[2]) {
$info['region'] = $addr[1];
$info['city'] = $addr[2];
if ($addr[1] == '') {
$addr[1] = '北京';
}
$cityLoc = getLoc($addr[1], $addr[2]);
$info['region'] = $cityLoc['region'];
$info['city'] = $cityLoc['city'];
$info['loc'] = $cityLoc['lat'] . ',' . $cityLoc['lon'];
}
}
$info['scope'] = tryCIDR($detail['beginIP'], $detail['endIP']);
$info['detail'] = $detail['dataA'] . $detail['dataB'];
if (filter_var($ip, \FILTER_VALIDATE_IP,\FILTER_FLAG_IPV4)) {
$qqwry = new QQWry('qqwry.dat');
$detail = $qqwry->getDetail($ip);
$info['scope'] = tryCIDR($detail['beginIP'], $detail['endIP']);
$info['detail'] = $detail['dataA'] . $detail['dataB'];
} else {
$info['scope'] = $info['ip'];
$info['detail'] = $info['as'] . ' ' . $info['isp'];
}
if ($_GET['cli'] == "true") { // 使用命令行模式
$cli = "IP: ".$info['ip'] . PHP_EOL;

Loading…
Cancel
Save