~~~
/**
* 獲取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" }
獲取的時間戳為當前時間戳的前一天