官方url:https://lbs.qq.com/service/webService/webServiceGuide/webServiceIp
php调用例子:
function ding(){
$url='https://apis.map.qq.com/ws/location/v1/ip?ip=117.189.52.150&key=您的key';
$res=curl_get($url);
$user_json = json_decode($res,true); //数据转换
//var_dump($user_json);//位置
print_r($user_json);
echo '<br/>';
$province = $user_json['result']['ad_info']['province'];
$city = $user_json['result']['ad_info']['city'];
echo 'province:'.$province.',city:'.$city;
}
function curl_get($url) {
$ch = curl_init();
$timeout = 300;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$res = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $res;
}
ding();
运行结果:
Array ( [status] => 0 [message] => query ok [result] => Array ( [ip] => 117.189.52.150 [location] => Array ( [lat] => 27.72545 [lng] => 106.92723 ) [ad_info] => Array ( [nation] => 中国 [province] => 贵州省 [city] => 遵义市 [district] => [adcode] => 520300 ) ) )
province:贵州省,city:遵义市