得到ip地理信息的幾種方法

目前已知的有騰訊、新浪、網易、搜狐和Google提供IP地址查詢API,可是找獲得的只有騰訊、新浪和網易的,Google的貌似要用Google Maps因此沒有研究。看了下國內的幾個騰訊提供的是JavaScript的,網易提供的是XML,而新浪的有多種格式能夠用,注意非XML的數據源都是GBK格式的,無論是JavaScript調用仍是PHP調用都要轉換一下編碼,否則獲得的是亂碼。而更須要注意的是,若是一次性查詢多個IP,使用門戶網站的API來查詢會很是緩慢,我大概寫了個for循環試了下,無論是用PHP解析XML仍是file_get_contents()函數獲取內容,查詢10次以上會變得很是緩慢,甚至可能超時。
騰訊的IP地址API接口地址:http://fw.qq.com/ipaddress,返回的是數據格式爲:var IPData = new Array("123.124.2.85","","北京市","");,一個JavaScript的對象,目前還不知道如何輸入IP查詢。
新浪的IP地址查詢接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
新浪多地域測試方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=123.124.2.85
網易有道的IP地址查詢接口:http://www.youdao.com/smartresult-xml/search.s?type=ip&q=123.124.2.85

使用JS代碼進行調取騰訊的api接口:
查看源代碼打印幫
<script language="javascript" type="text/javascript" src="http://fw.qq.com/ipaddress"></script>


<script>document.write("你的IP是:"+IPData[0]+",來自:"+IPData[2]);</script>
//騰訊API的PHP調用方法
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);
//調用查詢接口須要抓取網頁,有三種方法,第一種是curl,第二種是
//file_get_contents,第三種fopen->fread->fclose,推薦第二種方法
/*
*根據騰訊IP分享計劃的地址獲取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) ; // 獲取數據返回
$result = curl_exec($ch);
$result = mb_convert_encoding($result, "utf-8", "gb2312"); // 編碼轉換,不然亂碼
   curl_close($ch);
preg_match("@<span>(.*)</span></p>@iU",$result,$ipArray);
$loc = $ipArray[1];
return $loc;
}


//根據騰訊接口查詢ip地址,使用file_get_contents抓去網頁
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"); // 編碼轉換,不然亂碼
preg_match("@<span>(.*)</span></p>@iU",$result,$ipArray);
$loc = $ipArray[1];
return $loc;
}
//根據騰訊接口查詢ip地址,使用fopen->fread->fclose抓去網頁
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"); // 編碼轉換,不然亂碼
preg_match("@<span>(.*)</span></p>@iU",$result,$ipArray);
$loc = $ipArray[1];
return $loc;
}


/********注:
1.使用file_get_contents和fopen必須空間開啓allow_url_fopen。方法:編輯php.ini,設置allow_url_fopen = On,allow_url_fopen關閉時fopen和file_get_contents都不能打開遠程文件。
2.使用curl必須空間開啓curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分號去掉,而 且須要拷貝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安裝curl擴展*****/
//新浪查詢ip接口  第五個第六個是地理信息
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調用方法
$url = "http:www.youdao.com/smartresult-xml/search.s?type=ip&q=".$ip;
$doc = new DOMDocument();
   $doc->load($url);
   $smartresult = $doc->getElementsByTagName("product");
   foreach($smartresult as $product)
   {
      $locations = $product->getElementsByTagName("location");
      $location = $locations->item(0)->nodeValue;
   }
   if($location != "")
   {
       echo $i.".".$ip;
       echo "  來自".$location."的網友";
   }
   else
   {
       echo $i.".".$ip;
       echo "  來自火星的網友";
   }
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是一個很是棒的二維數組
其中有道和新浪的是我本身寫的,新浪API也能夠像騰訊API那樣用file_get_contents()函數獲取完地址後使用一連串的字符串函數處理,我寫的函數使用正則表達式重新浪的返回結果中提供包含中文的字符串,而且分段存入一個二維數組,這個可能只是針對新浪的API有用而且存在bug。舉個例子查詢學校分配給個人IP地址後var_dump()一下函數裏面的$get變量獲得如下結果: array(1) { [0]=> array(6) { [0]=> string(6) "中國" [1]=> string(6) "北京" [2]=> string(6) "北京" [3]=> string(9) "教育網" [4]=> string(6) "學校" [5]=> string(18) "中國地質大學" } },而函數輸出的結果則是「中國北京北京教育網學校中國地質大學」,但願個人思路和方法能對別人有用。
最後再次提醒,若是是WordPress請使用第一種方法,不然使用API同時查詢全部留言者的真實地址會讓PHP超時的,但願各路大牛有更好的方法,至於限制顯示和顯示方式等神馬的都是WordPress應用問題,同時對於Java和C#來講思路也是同樣的,這些後續的問題等我考完試再細說。javascript

相關文章
相關標籤/搜索