Skip to content

Commit b8ad4fe

Browse files
committed
支持传入自定义数据库文件路径,更新数据库
1 parent f94685e commit b8ad4fe

4 files changed

Lines changed: 21 additions & 15 deletions

File tree

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
## 数据库文件更新日期
1414

15-
2017年9月12日
15+
2018年2月21日
1616

1717
## 使用说明
1818

@@ -27,16 +27,16 @@ composer require 'itbdw/ip-database'
2727

2828
use itbdw\Ip\IpLocation;
2929

30-
$ip = '140.205.172.5'; //阿里云官网 ip
30+
//支持自定义文件路径
31+
$qqwry_filepath = '/abspath/qqwry.dat';
32+
echo json_encode(IpLocation::getLocation($ip, $qqwry_filepath), JSON_UNESCAPED_UNICODE) . "\n";
3133
echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n";
34+
35+
$ip = '140.205.172.5'; //阿里云官网 ip
3236
//{"ip":"140.205.172.5","country":"中国","province":"浙江","city":"杭州市","county":"","isp":"","area":"中国浙江杭州市阿里巴巴网络有限公司BGP数据中心"}
3337

3438
$ip = '172.217.27.142';//Google Ip
35-
echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n";
3639
//{"ip":"172.217.27.142","country":"美国","province":"","city":"","county":"","isp":"","area":"美国加利福尼亚州圣克拉拉县山景市谷歌公司"}
37-
38-
39-
4040
```
4141

4242
## 响应
@@ -63,10 +63,11 @@ echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n";
6363

6464
## 自己手动更新数据库
6565

66-
http://www.cz88.net/fox/ipdat.shtml
66+
1,http://www.cz88.net/fox/ipdat.shtml
6767
下载数据库程序(Windows 环境),执行完毕后,把 qqwry.dat 复制到 src 目录,覆盖掉原文件即可
6868

69-
69+
2,进一步,可以定期把上述文件同步到服务器特定路径,调用时动态传入真实 qqwry.dat 的绝对路径,
70+
而不是替换代码文件
7071

7172
## Thanks
7273

src/IpLocation.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,17 @@ class IpLocation
121121
*
122122
* @return IpLocation
123123
*/
124-
private final function __construct()
124+
private final function __construct($filepath = null)
125125
{
126-
$this->init();
126+
$this->init($filepath);
127127
}
128128

129-
private function init()
129+
private function init($filepath)
130130
{
131131
$filename = __DIR__ . '/qqwry.dat';
132+
if ($filepath) {
133+
$filename = $filepath;
134+
}
132135

133136
if (!file_exists($filename)) {
134137
trigger_error("Failed open ip database file!");
@@ -161,10 +164,10 @@ private function getlong()
161164
* @param $ip
162165
* @return array
163166
*/
164-
public static function getLocation($ip)
167+
public static function getLocation($ip, $filepath = null)
165168
{
166169
if (self::$instance === null) {
167-
self::$instance = new self();
170+
self::$instance = new self($filepath);
168171
}
169172

170173
return self::$instance->getAddr($ip);

src/qqwry.dat

34.3 KB
Binary file not shown.

tests/ip.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
use itbdw\Ip\IpLocation;
1111

1212
$hostnames = [
13-
'aliyun.com',
1413
'google.com',
1514
'weibo.com',
16-
'invalidip',
15+
'nothing',
1716
];
1817
shuffle($hostnames);
1918
$hostname = array_pop($hostnames);
@@ -22,5 +21,8 @@
2221

2322
echo $hostname . "\n";
2423

24+
$qqwry_path = dirname(__DIR__) . '/src/qqwry.dat';
25+
2526
echo json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE) . "\n";
27+
echo json_encode(IpLocation::getLocation($ip, $qqwry_path), JSON_UNESCAPED_UNICODE) . "\n";
2628

0 commit comments

Comments
 (0)