php用IP查询归属地

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php用IP查询归属地相关的知识,希望对你有一定的参考价值。

php用IP查询归属地,现在网上搜索一看的话都在用QQwryIP库。
想知道的是这个库准吗?
还有类似这样的库吗?
推荐个最准的库吧,谢谢。

还有想了解用IP查询国家的方法。
这是怎么获取的??
每个国家都有规律的吗??
高手们请指教。

参考技术A class ip_location

function init()

$this->wrydat = 'ip_area.dat';

$this->fp = fopen($this->wrydat, 'rb');
$this->getipnumber();
$this->getwryversion();

$this -> REDIRECT_MODE_0 = 0;
$this -> REDIRECT_MODE_1 = 1;
$this -> REDIRECT_MODE_2 = 2;


function get($str)

return $this->$str;


function set($str,$val)

$this->$str = $val;


function getbyte($length,$offset=null)

!is_null($offset) && fseek($this->fp, $offset, SEEK_SET);

return fread($this->fp, $length);


function packip($ip)

return pack('N', intval(ip2long($ip)));


function getlong($length=4, $offset=null)

$chr=null;
for($c=0;$length%4!=0&&$c<(4-$length%4);$c++)

$chr .= chr(0);

$var = unpack( 'Vlong', $this->getbyte($length, $offset).$chr);
return $var['long'];


function getwryversion()

$length = preg_match("/coral/i",$this->wrydat)?26:30;
$this->wrydat_version = $this->getbyte($length, $this->firstip-$length);


function getipnumber()

$this->firstip = $this->getlong();
$this->lastip = $this->getlong();
$this->ipnumber = ($this->lastip-$this->firstip)/7+1;


function getstring($data='', $offset=NULL)

$char = $this->getbyte(1,$offset);
while(ord($char) > 0)

$data .= $char;
$char = $this->getbyte(1);

return $data;


function iplocaltion($ip)

$ip = $this->packip($ip);
$low = 0;
$high = $this->ipnumber-1;
$ipposition = $this->lastip;
while($low <= $high)

$t = floor(($low+$high)/2);
if($ip < strrev($this->getbyte(4,$this->firstip+$t*7)))
$high = $t - 1;
else

if($ip > strrev($this->getbyte(4,$this->getlong(3))))
$low = $t + 1;
else

$ipposition = $this->firstip+$t*7;
break;



return $ipposition;


function getarea()

$b = $this->getbyte(1);
switch(ord($b))

case $this -> REDIRECT_MODE_0 :
return '';
break;
case $this -> REDIRECT_MODE_1:
case $this -> REDIRECT_MODE_2:
return $this->getstring('',$this->getlong(3));
break;
default:
return $this->getstring($b);
break;



function getiplocation($ip)

$ippos = $this->iplocaltion($ip);
$this->ip_range_begin = long2ip($this->getlong(4,$ippos));
$this->ip_range_end = long2ip($this->getlong(4,$this->getlong(3)));
$b = $this->getbyte(1);
switch(ord($b))

case $this -> REDIRECT_MODE_1:
$b = $this->getbyte(1,$this->getlong(3));
if(ord($b) == $this -> REDIRECT_MODE_2)

$countryoffset = $this->getlong(3);
$this->area = $this->getarea();
$this->country = $this->getstring('',$countryoffset);

else

$this->country = $this->getstring($b);
$this->area = $this->getarea();

break;
case $this -> REDIRECT_MODE_2:
$countryoffset = $this->getlong(3);
$this->area = $this->getarea();
$this->country = $this->getstring('',$countryoffset);
break;
default:
$this->country = $this->getstring($b);
$this->area = $this->getarea();
break;




---------------------------------------------------------------

调用方法:

$iploca = new ip_location;
$iploca -> init();
$iploca -> getiplocation($ip);

$area['country'] = str_replace(array('CZ88.NET'), '', $iploca -> get('country'));
$area['area'] = str_replace(array('CZ88.NET'), '', $iploca -> get('area'));

$area['country']=='' && $area['country']='未知';
$area['area']=='' && $area['area']='未知';
return $area;本回答被提问者采纳

批量查询ip地址归属地

 1 #!/usr/bin/env python
 2 #-*-coding:utf-8-*-
 3 import urllib
 4 import urllib2
 5 import json
 6 import time
 7 import sys
 8 def check_ipaddr(func,*args, **kwargs):
 9     iplist = func(*args,**kwargs)
10     temp_list =[]
11     for line in iplist:
12         if line.split():
13             a = json.loads(line)
14             b = []
15             for item in a:
16                 if item not in b:
17                     b.append(item)
18             b = "".join(b)
19             temp_list.append(b)
20     return temp_list
21 
22 def check_url(res):
23     ip_list = []
24     f = open(res,r)
25     for line in f:
26         url = "http://freeapi.ipip.net/%s" %line.strip()
27         ip_addr = urllib.urlopen(url).read()
28         time.sleep(0.25)
29         ip_list.append(ip_addr)
30     f.flush()
31     #print len(ip_list)
32     return ip_list
33 
34 def ip_city(ip_file):
35     ip_list = []
36     city_list = []
37     temp =check_ipaddr(check_url,ip_file)
38     f = open(ip_file,r)
39     for i in f:
40         line = i.strip().split(\n)
41         for i in line:
42             ip_list.append(i)
43             #f.flush()
44     for line in temp:
45         city_list.append(line)
46     for i in range(len(ip_list)):
47         l =i+1
48         time.sleep(0.07)
49         print "%s)"%l,ip_list[i],"\t"+"("+ city_list[i] +")"
50     
51     f.flush
52     return ip_city
53 
54 if __name__ == "__main__":
55     if len(sys.argv) >1:
56         ip_city(sys.argv[1])
57     else:
58         print "\033[31;5mPlease pass a file....\033[0m"

 

以上是关于php用IP查询归属地的主要内容,如果未能解决你的问题,请参考以下文章

php 手机号归属地显示查询

在表格中批量查询IP地址归属地

IP地址批量查询归属地在线工具

如何利用 IP 归属地查询 API 精准锁定用户位置

python爬取免费优质IP归属地查询接口

批量查询ip地址归属地