JS PHP获取所在城市

JS

var province = '' ;
var city = '' ;
jQuery.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js",function(){
      province = remote_ip_info["province"];
      city = remote_ip_info["city"];  
      alert(city)
 }) ;

PHP

//根据现有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;  
} 
$ipInfos = GetIpLookup('123.125.114.144'); //baidu.com IP地址  //ipInfos 是一个数组
var_dump($ipInfos);

你可能感兴趣的:(JS PHP获取所在城市)