php獲取ip地理資訊幾種方法

WBOY
發布: 2016-07-25 09:12:53
原創
1116 人瀏覽過
舉個例子,查詢學校分配給我的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) "中國地質大學" } },而函數輸出的結果則是“中國北京北京教育網學校中國地質大學”,希望我的想法和方法對別人有用。

方法一,利用純真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介面:

  1. <script>document.write("你的IP是:"+IPData[0]+",來自:"+IPData[2]);</script>
  2. //騰訊API的PHP呼叫方法
  3. function getIpPlace(){
  4. $ip=file_get_contents("http://fw.qq.com/ipaddress");
  5. $ip=str_replace('"' ,' ',$ip);
  6. $ip2=explode("(",$ip);
  7. $a=substr($ip2[1],0,-2);
  8. $b= explode(",",$a);
  9. return $b;
  10. }
  11. $ip=getIpPlace();
  12. print_r($ip);
  13. //呼叫查詢介面需要抓拿網頁,有三種方法,第一種是curl,第二種是
  14. //file_get_contents,第三種fopen->fread->fclose,推薦第二種方法
  15. /*
  16. *根據騰訊IP分享計畫的位址取得IP所在地,較精確
  17. */
  18. function getIPLoc($queryIP){
  19. $url = 'http://ip.qq.com/cgi-bin/searchip? searchip1='.$queryIP;
  20. $ch = curl_init($url);
  21. curl_setopt($ch,CURLOPT_ENCODING ,'gb2312');
  22. curl_setopt($ch, CUR 312');
  23. curl_setopt($ch, CUR PT_TIME. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 取得資料回傳
  24. $result = curl_exec($ch);
  25. $result = mb_convert_encoding($result, "utf-8", "gb2312"); / 編碼轉換,否則亂碼
  26. curl_close($ch);
  27. preg_match("@(.*)@iU",$result,$ipArray);
  28. $loc = $ipArray[1];
  29. return $loc;
  30. }
  31. //根據騰訊介面查詢ip位址,使用file_get_contents抓去網頁

  32. function getIPLoc ($queryIP){
  33. $url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryIP;
  34. $result = file_get_contents($url);
  35. $result = mb_convert_encoding($result, "utf-8", "gb2312"); // 編碼轉換,否則亂碼
  36. preg_match("@(.*)@iU ",$result,$ipArray);
  37. $loc = $ipArray[1];
  38. return $loc;
  39. }
  40. //根據騰訊介面查詢ip位址,使用fopen->fread- >fclose抓去網頁
  41. function getIPLoc($queryIP){
  42. $url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryIP;
  43. $handle = fopen ("$url", "rb");
  44. $result = "";
  45. do {
  46. $data = fread($handle, 1024);
  47. if (strlen($data ) == 0) {
  48. break;
  49. }
  50. $result .= $data;
  51. } while(true);
  52. $result = mb_convert_encoding($result, "utf-8" , "gb2312"); // 編碼轉換,否則亂碼
  53. preg_match("@(.*)@iU",$result,$ipArray);
  54. $ loc = $ipArray[1];
  55. return $loc;
  56. }
複製程式碼
複製程式碼

/********註: 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:WINDOWSsystem32下;Linux下要安裝curl

DOWSsystem32下;Linux下要安裝curl
  1. 擴充*****/
  2. //新浪查詢ip介面第五個第六個是地理資訊
  3. function getiploc($IP_ip){
  4. $IP_str = @file_get_contents('http ://int.dpool.sina.com.cn/iplookup/iplookup.php?ip='.$IP_ip);
  5. if(!empty($IP_str)){
  6. $IP_tmp = explode(" " , $IP_str);
  7. $IP_city = iconv("GBK", "UTF-8", $IP_tmp[5]);
  8. return $IP_city;
  9. }
  10. //有道API的PHP呼叫方法
  11. $url = "http:www.youdao.com/smartresult-xml/search.s?type=ip&q=".$ip;
  12. $doc = new DOMDocument();
  13. $doc->load($url);
  14. $smartresult = $doc->getElementsByTagName("product");
  15. foreach($smartresult as $product)
  16. {
  17. $locations = $product->getElementsByTagName("location");
  18. $location = $locations->item(0)->nodeValue;
  19. }
  20. if($location != "")
  21. {
  22. echo $i.".".$ip;
  23. echo " 來自".$location."的網友";
  24. }
  25. else
  26. {
  27. echo $i.". ".$ip;
  28. echo " 來自火星的網友";
  29. }
  30. public function sinaIPApi($ip){
  31. $str = file_get_contents("http://int.dpool.sina. com.cn/iplookup/iplookup.php?ip=".$ip);
  32. $str = iconv("gbk", "utf-8//IGNORE", $str);
  33. preg_match_all("/ [x{4e00}-x{9fa5}]+/u",$str,$get);
  34. $add = implode('',$get[0]);
  35. return $add;
}
//$get是一個非常棒的二維陣列

複製程式碼

新浪API也可以像騰訊API一樣用file_get_contents ()函數取得完位址後使用一連串的字串函數處理,使用正規表示式從新浪的回傳結果提供包含中文的字串,並且分段存入一個二維數組,這個可能只是針對新浪的API有用並且存在bug。


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!