~~~
/**
* 获取ip地址所在的区域
* @param null $ip
* @return bool|mixed
* @author:xjw129xjt(肖骏涛) xjt@ourstu.com
*/
function get_ip_lookup($ip=null){
if(empty($ip)){
$ip = get_client_ip(0);
}
$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;
}
~~~
~~~
/**
* 获取ip地址所在的区域
* @param string $ip
* @return string
*/
function get_city_by_ip($ip){
$url = "http://ip.taobao.com/service/getIpInfo.php?ip=" . $ip;
$ipinfo = json_decode(file_get_contents($url));
if ($ipinfo->code == '1') {
return false;
}
$city = $ipinfo->data->region . $ipinfo->data->city; //省市县
$ip = $ipinfo->data->ip; //IP地址
$ips = $ipinfo->data->isp; //运营商
$guo = $ipinfo->data->country; //国家
if ($guo == '中国') {
$guo = '';
}
return $guo . $city . $ips . '[' . $ip . ']';
}
~~~
var_dump(get_ip_lookup('58.210.11.194'));
**输出如下:**
> array(10) { ["start"]=> int(-1) ["end"]=> int(-1) ["country"]=> string(6) "中国" ["province"]=> string(6) "江苏" ["city"]=> string(6) "苏州" ["district"]=> string(0) "" ["isp"]=> string(0) "" ["type"]=> string(0) "" ["desc"]=> string(0) "" ["ip"]=> string(13) "58.210.11.194" }
获取的时间戳为当前时间戳的前一天