第一種是利用純真ip數(shù)據(jù)庫(kù),這個(gè)可以在網(wǎng)上找到很多,缺點(diǎn)是更新有點(diǎn)慢。
第二種是利用門(mén)戶(hù)網(wǎng)站的接口
目前已知的有騰訊、新浪、網(wǎng)易、搜狐和Google提供IP地址查詢(xún)API,但是找得到的只有騰訊、新浪和網(wǎng)易的,Google的貌似要用Google Maps所以沒(méi)有研究??戳讼聡?guó)內(nèi)的幾個(gè)騰訊提供的是JavaScript的,網(wǎng)易提供的是XML,而新浪的有多種格式可以用,注意非XML的數(shù)據(jù)源都是GBK格式的,不管是JavaScript調(diào)用還是PHP調(diào)用都要轉(zhuǎn)換一下編碼,不然得到的是亂碼。而更需要注意的是,如果一次性查詢(xún)多個(gè)IP,使用門(mén)戶(hù)網(wǎng)站的API來(lái)查詢(xún)會(huì)非常緩慢,我大概寫(xiě)了個(gè)for循環(huán)試了下,不管是用PHP解析XML還是file_get_contents()函數(shù)獲取內(nèi)容,查詢(xún)10次以上會(huì)變得非常緩慢,甚至可能超時(shí)。
騰訊的IP地址API接口地址:http://fw.qq.com/ipaddress,返回的是數(shù)據(jù)格式為:var IPData = new Array(“123.124.2.85″,”",”北京市”,”");,一個(gè)JavaScript的對(duì)象,目前還不知道如何輸入IP查詢(xún)。
新浪的IP地址查詢(xún)接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
新浪多地域測(cè)試方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=jsip=123.124.2.85
網(wǎng)易有道的IP地址查詢(xún)接口:http://www.youdao.com/smartresult-xml/search.s?type=ipq=123.124.2.85
使用JS代碼進(jìn)行調(diào)取騰訊的api接口:
查看源代碼打印幫
復(fù)制代碼 代碼如下:
script language=”javascript” type=”text/javascript” src=”http://fw.qq.com/ipaddress”>/script>
script>document.write(“你的IP是:”+IPData[0]+”,來(lái)自:”+IPData[2]);/script>
//騰訊API的PHP調(diào)用方法
function getIpPlace(){
$ip=file_get_contents(“http://fw.qq.com/ipaddress”);
$ip=str_replace(‘”‘,' ‘,$ip);
$ip2=explode(“(“,$ip);
$a=substr($ip2[1],0,-2);
$b=explode(“,”,$a);
return $b;
}
$ip=getIpPlace();
print_r($ip);
//調(diào)用查詢(xún)接口需要抓取網(wǎng)頁(yè),有三種方法,第一種是curl,第二種是
//file_get_contents,第三種fopen->fread->fclose,推薦第二種方法
/*
*根據(jù)騰訊IP分享計(jì)劃的地址獲取IP所在地,比較精確
*/
function getIPLoc($queryIP){
$url = ‘http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryIP;
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_ENCODING ,'gb2312′);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 獲取數(shù)據(jù)返回
$result = curl_exec($ch);
$result = mb_convert_encoding($result, “utf-8″, “gb2312″); // 編碼轉(zhuǎn)換,否則亂碼
curl_close($ch);
preg_match(“@span>(.*)/span>/p>@iU”,$result,$ipArray);
$loc = $ipArray[1];
return $loc;
}
//根據(jù)騰訊接口查詢(xún)ip地址,使用file_get_contents抓去網(wǎng)頁(yè)
function getIPLoc($queryIP){
$url = ‘http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryIP;
$result = file_get_contents($url);
$result = mb_convert_encoding($result, “utf-8″, “gb2312″); // 編碼轉(zhuǎn)換,否則亂碼
preg_match(“@span>(.*)/span>/p>@iU”,$result,$ipArray);
$loc = $ipArray[1];
return $loc;
}
//根據(jù)騰訊接口查詢(xún)ip地址,使用fopen->fread->fclose抓去網(wǎng)頁(yè)
function getIPLoc($queryIP){
$url = ‘http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryIP;
$handle = fopen (“$url”, “rb”);
$result = “”;
do {
$data = fread($handle, 1024);
if (strlen($data) == 0) {
break;
}
$result .= $data;
} while(true);
$result = mb_convert_encoding($result, “utf-8″, “gb2312″); // 編碼轉(zhuǎn)換,否則亂碼
preg_match(“@span>(.*)/span>/p>@iU”,$result,$ipArray);
$loc = $ipArray[1];
return $loc;
}
/********注:
1.使用file_get_contents和fopen必須空間開(kāi)啟allow_url_fopen。方法:編輯php.ini,設(shè)置allow_url_fopen = On,allow_url_fopen關(guān)閉時(shí)fopen和file_get_contents都不能打開(kāi)遠(yuǎn)程文件。
2.使用curl必須空間開(kāi)啟curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分號(hào)去掉,而 且需要拷貝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安裝curl擴(kuò)展*****/
//新浪查詢(xún)ip接口 第五個(gè)第六個(gè)是地理信息
function getiploc($IP_ip){
$IP_str = @file_get_contents(‘http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip='.$IP_ip);
if(!empty($IP_str)){
$IP_tmp = explode(” ”, $IP_str);
$IP_city = iconv(“GBK”, “UTF-8″, $IP_tmp[5]);
return $IP_city;
}
//有道API的PHP調(diào)用方法
$url = “http:www.youdao.com/smartresult-xml/search.s?type=ipq=”.$ip;
$doc = new DOMDocument();
$doc->load($url);
$smartresult = $doc->getElementsByTagName(“product”);
foreach($smartresult as $product)
{
$locations = $product->getElementsByTagName(“l(fā)ocation”);
$location = $locations->item(0)->nodeValue;
}
if($location != “”)
{
echo $i.”.”.$ip;
echo ” 來(lái)自”.$location.”的網(wǎng)友”;
}
else
{
echo $i.”.”.$ip;
echo ” 來(lái)自火星的網(wǎng)友”;
}
public function sinaIPApi($ip){
$str = file_get_contents(“http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=”.$ip);
$str = iconv(“gbk”, “utf-8//IGNORE”, $str);
preg_match_all(“/[\x{4e00}-\x{9fa5}]+/u”,$str,$get);
$add = implode(”,$get[0]);
return $add;
}
//$get是一個(gè)非常棒的二維數(shù)組
其中有道和新浪的是我自己寫(xiě)的,新浪API也可以像騰訊API那樣用file_get_contents()函數(shù)獲取完地址后使用一連串的字符串函數(shù)處理,我寫(xiě)的函數(shù)使用正則表達(dá)式從新浪的返回結(jié)果中提供包含中文的字符串,并且分段存入一個(gè)二維數(shù)組,這個(gè)可能只是針對(duì)新浪的API有用并且存在bug。舉個(gè)例子查詢(xún)學(xué)校分配給我的IP地址后var_dump()一下函數(shù)里面的$get變量得到以下結(jié)果: array(1) { [0]=> array(6) { [0]=> string(6) “中國(guó)” [1]=> string(6) “北京” [2]=> string(6) “北京” [3]=> string(9) “教育網(wǎng)” [4]=> string(6) “學(xué)校” [5]=> string(18) “中國(guó)地質(zhì)大學(xué)” } },而函數(shù)輸出的結(jié)果則是“中國(guó)北京北京教育網(wǎng)學(xué)校中國(guó)地質(zhì)大學(xué)”,希望我的思路和方法能對(duì)別人有用。
最后再次提醒,如果是WordPress請(qǐng)使用第一種方法,否則使用API同時(shí)查詢(xún)所有留言者的真實(shí)地址會(huì)讓PHP超時(shí)的,希望各路大牛有更好的方法,至于限制顯示和顯示方式等神馬的都是WordPress應(yīng)用問(wèn)題,同時(shí)對(duì)于Java和C#來(lái)說(shuō)思路也是一樣的,這些后續(xù)的問(wèn)題等我考完試再細(xì)說(shuō)。