1. <?php
  2.     header("Content-type: text/html; charset=utf-8"); 
  3.     //获取IP地址的方法
  4.     function getIP(){
  5.         if (isset($_SERVER)) {
  6.             if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  7.                 $realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  8.             } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  9.                 $realip = $_SERVER['HTTP_CLIENT_IP'];
  10.             } else {
  11.                 $realip = $_SERVER['REMOTE_ADDR'];
  12.             }
  13.         } else {
  14.             if (getenv("HTTP_X_FORWARDED_FOR")) {
  15.                 $realip = getenv( "HTTP_X_FORWARDED_FOR");
  16.             } elseif (getenv("HTTP_CLIENT_IP")) {
  17.                 $realip = getenv("HTTP_CLIENT_IP");
  18.             } else {
  19.                 $realip = getenv("REMOTE_ADDR");
  20.             }
  21.         }
  22.         return $realip;
  23.     }
  24.     
  25.     echo $ip = getIP();

  26. //通过php的file_get_contents()方法获取地理位置
  27. //新浪接口根据ip查询所在区域信息

  28. $res0 = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip");
  29. $res0 = json_decode($res0,true);
  30. print_r($res0);
  31. echo "<br/>";

  32. //淘宝接口根据ip查询所在区域信息

  33. $res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip");
  34. $res1 = json_decode($res1,true);
  35. print_r($res1);
  36. echo "<br/>";


  37. //通过php的curl获取地理位置
  38. //新浪根据IP获取地理位置API

  39.     $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip'; 
  40.     $ch = curl_init($url); 
  41.     curl_setopt($ch,CURLOPT_ENCODING ,'utf8'); 
  42.     curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
  43.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回 
  44.     $location = curl_exec($ch); 
  45.     $location = json_decode($location); 
  46.     print_r($location);
  47.     curl_close($ch); 
  48.      
  49.     $loc = ""; 
  50.     if($location===FALSE) return ""; 
  51.     if (empty($location->desc)) { 
  52.         $loc = $location->province.$location->city.$location->district.$location->isp; 
  53.     }else{ 
  54.         $loc = $location->desc; 
  55.     } 
  56.     echo  $loc; 


  57. //腾讯根据IP获取地理位置API

  58.     $url = 'http://ip.qq.com/cgi-bin/searchip?searchip1=$ip'; 
  59.     $ch = curl_init($url); 
  60.     curl_setopt($ch,CURLOPT_ENCODING ,'gb2312'); 
  61.     curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
  62.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回 
  63.     $result = curl_exec($ch); 
  64.     $result = mb_convert_encoding($result, "utf-8", "gb2312"); // 编码转换,否则乱码 
  65.     curl_close($ch); 
  66.     preg_match("@<span>(.*)</span></p>@iU",$result,$ipArray); 
  67.     $loc = $ipArray[1]; 
  68.     echo $loc;

相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!