根据ip获取位置信息,这里利用了淘宝api,速度比本地查询慢不了多少 /** * ip 地址定位 * @param string|integer $ip ip地址 * @param bool $number 是否数字(因此上一个参数为 ip2long 函数转换之后的长整型) * @return array 地址数据 * @throws exception 获取出错 */ function location($ip, $number = false) { //如果是数字形式的ip地址,则转换成标准ip if ($number) { $ip = long2ip($ip); } $api = 'http://ip.taobao.com/service/getipinfo.php'; $resp = file_get_contents($api.'?ip='.$ip); $resp = json_decode($resp,1); if($resp['code'] == 0){ return $resp['data']; }else{ throw new exception($resp['data']); } }
