php根据IP获取IP所在城市

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

 

posted @ 2016-11-07 13:52  jiafeng  阅读(5722)  评论(0编辑  收藏  举报