已知一个IP
$ipname=api_hits($DT_IP);
--------------
//api
function getAddressComponent($ak, $longitude, $latitude, $pois){
$param = array(
'ak' => $ak,
'location' => implode(',', array($latitude, $longitude)),
'pois' => $pois,
'output' => 'json'
);
// 请求百度api
$api = 'http://api.map.baidu.com/geocoder/v2/';
$response = toCurl($api, $param);
$result = array();
if($response){
$result = json_decode($response, true);
}
return $result;
}
function toCurl($url, $param=array()){
$ch = curl_init();
if(substr($url,0,5)=='https'){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
$response = curl_exec($ch);
if($error=curl_error($ch)){
return false;
}
curl_close($ch);
return $response;
}
function api_hits($ip){
$ak = "AAAAAAAAAAAKKKKKKKKKKKK";
$url="http://api.map.baidu.com/location/ip?ip=$ip&ak=AAAAAAAAKKKKKKKK&coor=bd09ll";
// $url="http://api.map.baidu.com/location/ip?ip=27.184.129.10&ak=0ZcfSGCYyuvQNCbUXDPHBUIOes2rNEtW&coor=bd09ll";
$myip=file_get_contents($url);
$a=substr($myip, (stripos($myip, "x")+4));
$b=preg_match_all('/\d+/',$a,$arr);
$arr=$arr['0'];
$x=$arr['0'].".".$arr['1'];
$y=$arr['2'].".".$arr['3'];
$longitude = $x;
$latitude = $y;
$aa = getAddressComponent($ak,$longitude,$latitude,1);
// var_dump($aa);
$areaname = $aa['result']['addressComponent']['province'];
// $areaname=substr($areaname, (stripos($areaname, "省")));
// $areaname = stripos($areaname, "省");
$areaname =mb_substr($areaname,0, -1);
return $areaname;
}