Skip to content

Commit

Permalink
兼容 上海交通大学 类似的数据
Browse files Browse the repository at this point in the history
  • Loading branch information
zhao.binyan committed Aug 1, 2019
1 parent 9a0030d commit 134835a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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":"美国加利福尼亚州圣克拉拉县山景市谷歌公司"}
Expand All @@ -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`

### 【或者】自己手动更新数据库

Expand Down
36 changes: 22 additions & 14 deletions src/IpLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,28 +251,37 @@ 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;

//没有省份标志 只能替换
$_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) {
Expand Down Expand Up @@ -325,7 +334,6 @@ private function getAddr($ip)

$result['area'] = $location['country'] . $location['province'] . $location['city'] . $location['county'] . $location['org_area'];
}

return $result; //array
}

Expand Down
24 changes: 22 additions & 2 deletions tests/ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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";
}


0 comments on commit 134835a

Please sign in to comment.