您的位置:首页 > 编程语言 > PHP开发

php中获取ip的地域信息比较精准

2011-12-26 14:26 495 查看
这些是本人的劳动成果,放在这里一是作为备忘,二是共享给需要的朋友们。如果好用,就说声谢谢,我会很高兴的。如果不能用,请给我留言。
/**
  *功能:
  *		根据ip获得国家,省,城市,运营商
  *备注:
  *		利用的是新浪的ip查询接口 gb2312
  *编写人:jiftle
  *编写时间:11:17 2011年12月26日星期二
 **/
  function ip_Place_Array($ipAddr){
           // $ipAddr = "218.75.124.100";
           //http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=218.75.124.100
           //1	218.75.123.215	218.75.127.243	中国	浙江	杭州	 电信
           //http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=60.185.82.120
           //1	60.185.58.0	60.185.127.255	中国	浙江	衢州		电信
           //http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=66.85.151.82
           //1	66.85.0.0	66.85.255.255	美国	得克萨斯州	 El Paso
           //http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=188.138.84.132
           //1	188.138.62.160	188.138.127.255	德国	nordrhein-westfalen	 Hürth
           //http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=106.187.35.135
           //1	106.128.0.0	106.191.255.255	日本
            $ip138Addr = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=".$ipAddr;
            $contents = file_get_contents($ip138Addr);
			$contents=iconv("GB2312", "UTF-8//IGNORE", $contents);//编码转换
			//echo $contents;
			$arTmp = explode("\t",$contents); //这里是让我感到奇怪的城市后面的字段
			$arLocation = array($ipAddr,$arTmp[3],$arTmp[4],$arTmp[5],$arTmp[7]);

            return $arLocation;
         }


/**
  *功能:
  *		根据ip获得国家,省,城市,运营商
  *备注:
  *		利用的ip138的ip查询接口 gb2312
  *编写人:jiftle
  *编写时间:13:10 2011年12月26日星期二
 **/
		//http://www.ip138.com/ips8.asp?ip=58.18.32.195&action=2
    function ip_Place($ipAddr){
            //$ipAddr = "218.75.124.100";
           
            $ip138Addr = "http://www.ip138.com/ips8.asp?ip=".$ipAddr."&action=2";
            $contents = file_get_contents($ip138Addr);
            $contents=iconv("GB2312", "UTF-8//IGNORE", $contents);//编码转换
            
            $intStart = strpos($contents,"<li>")+4;
            $intEnd = strpos($contents,"</li>");
            $place = substr($contents,$intStart,$intEnd-$intStart);
            $intStart = strpos($place,"本站主数据:")+18;
            $intEndPos = 0;
            $intEndPos = strpos($place," ");
            if($intEndPos)
                $place = substr($place,$intStart,$intEndPos-$intStart);
            else
				$place = substr($place,$intStart,$intEnd-$intStart);
            return $place;
         }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: