diff --git a/README.md b/README.md index d2ea0e1..6085957 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,16 @@ echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n"; } ``` + +## 测试 + +``` +php tests/ip.php + +php tests/ip.php -i 58.196.128.0 + +``` + ## 典型返回 ``` {"ip":"172.217.25.14","country":"美国","province":"","city":"","county":"","isp":"","area":"美国加利福尼亚州圣克拉拉县山景市谷歌公司"} @@ -73,7 +83,7 @@ echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n"; `php ~/bin/update-ip.php` 更新到指定目录 -`php ~/bin/update-ip.php -d=/tmp` +`php ~/bin/update-ip.php -d /tmp` ### 【或者】自己手动更新数据库 diff --git a/src/IpLocation.php b/src/IpLocation.php index 9ec85cb..f424e1a 100644 --- a/src/IpLocation.php +++ b/src/IpLocation.php @@ -251,16 +251,25 @@ private function getAddr($ip) //直辖市 if (in_array($value, $this->dict_city_directly)) { $_tmp_province = explode($seperator_shi, $location['country']); - //直辖市 - $location['province'] = $_tmp_province[0]; - - //市辖区 - if (isset($_tmp_province[1])) { - if (strpos($_tmp_province[1], $seperator_qu) !== false) { - $_tmp_qu = explode($seperator_qu, $_tmp_province[1]); - $location['city'] = $_tmp_qu[0] . $seperator_qu; + + // 上海市浦江区xxx + if ($_tmp_province[0] == $value) { + //直辖市 + $location['province'] = $_tmp_province[0]; + + //市辖区 + if (isset($_tmp_province[1])) { + if (strpos($_tmp_province[1], $seperator_qu) !== false) { + $_tmp_qu = explode($seperator_qu, $_tmp_province[1]); + $location['city'] = $_tmp_qu[0] . $seperator_qu; + } } + } else { + //上海交通大学 + $location['province'] = $value; + $location['org_area'] = $location['org_country'] . $location['org_area']; } + } else { //省 $location['province'] = $value; @@ -268,11 +277,11 @@ private function getAddr($ip) //没有省份标志 只能替换 $_tmp_city = str_replace($location['province'], '', $location['country']); - //防止直辖市捣乱 上海市xxx区 =》 市xx区 - $_tmp_shi_pos = mb_stripos($_tmp_city, $seperator_shi); - if ($_tmp_shi_pos === 0) { - $_tmp_city = mb_substr($_tmp_city, 1); - } + //防止直辖市捣乱 上海市xxx区 =》 市xx区 + $_tmp_shi_pos = mb_stripos($_tmp_city, $seperator_shi); + if ($_tmp_shi_pos === 0) { + $_tmp_city = mb_substr($_tmp_city, 1); + } //内蒙古 类型的 获取市县信息 if (strpos($_tmp_city, $seperator_shi) !== false) { @@ -325,7 +334,6 @@ private function getAddr($ip) $result['area'] = $location['country'] . $location['province'] . $location['city'] . $location['county'] . $location['org_area']; } - return $result; //array } diff --git a/tests/ip.php b/tests/ip.php index 525f927..328a0f2 100644 --- a/tests/ip.php +++ b/tests/ip.php @@ -7,6 +7,8 @@ //you do not need to do this if use composer! require dirname(__DIR__) . '/src/IpLocation.php'; +$input = getopt("i:", ['ip:']); + use itbdw\Ip\IpLocation; $ips = [ @@ -15,12 +17,30 @@ "123.125.115.110",//北京 "221.196.0.0",// "60.195.153.98", + + //bug ip 都是涉及到直辖市的 + "218.193.183.35", //"province":"上海交通大学闵行校区", + "210.74.2.227", //,"province":"北京工业大学","city":"", + "162.105.217.0", //,"province":"北京大学万柳学区","ci + + + ]; -foreach ($ips as $ip) { +if (isset($input['i']) || isset($input['ip'])) { + $ips = []; - echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n"; + if (isset($input['i'])) { + $ips[] = $input['i']; + } + + if (isset($input['ip'])) { + $ips[] = $input['ip']; + } +} +foreach ($ips as $ip) { + echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n"; }