当前位置:Gxlcms > PHP教程 > php根据IP地址获取地理位置

php根据IP地址获取地理位置

时间:2021-07-01 10:21:17 帮助过:18人阅读

    ";//淘宝接口根据ip查询所在区域信息$res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip");$res1 = json_decode($res1,true);print_r($res1);echo "
";//通过php的curl获取地理位置//新浪根据IP获取地理位置API $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip'; $ch = curl_init($url); curl_setopt($ch,CURLOPT_ENCODING ,'utf8'); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回 $location = curl_exec($ch); $location = json_decode($location); print_r($location); curl_close($ch); $loc = ""; if($location===FALSE) return ""; if (empty($location->desc)) { $loc = $location->province.$location->city.$location->district.$location->isp; }else{ $loc = $location->desc; } echo $loc; //腾讯根据IP获取地理位置API $url = 'http://ip.qq.com/cgi-bin/searchip?searchip1=$ip'; $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("@(.*)

@iU",$result,$ipArray); $loc = $ipArray[1]; echo $loc;

人气教程排行