根据现有IP地址获取其地理位置(省份,城市等)的方法

根据现有IP地址获取其地理位置(省份,城市等)的方法

function GetIpLookup($ip = ''){    
    if(empty($ip)){    
       return '请输入IP地址';  
    }    
    $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);    
    if(empty($res)){ return false; }    
    $jsonMatches = array();    
    preg_match('#{.+?}#', $res, $jsonMatches);    
    if(!isset($jsonMatches[0])){ return false; }    
    $json = json_decode($jsonMatches[0], true);    
    if(isset($json['ret']) && $json['ret'] == 1){    
        $json['ip'] = $ip;    
        unset($json['ret']);    
    }else{    
        return false;    
    }    
    return $json;    
}

$arr=GetIpLookup($_SERVER["REMOTE_ADDR"]);

var_dump($arr);
原文地址:https://www.cnblogs.com/archoncap/p/4900149.html