通过手机号获取定位

function getMobileLoc($mobile){

$data = array('province'=>'','city'=>'');
$url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={$mobile}&resource_id=6004&ie=utf8&oe=utf8&format=json";

$result = curl_https($url);
$result = (array)json_decode($result);

if (!empty($result['data'])) {
$result = (array)$result['data'][0];
$data['city'] = empty($result['city']) ? '' : $result['city'];
$data['province'] = empty($result['prov']) ? '' : $result['prov'];
}

return $data['city'];
}
function curl_https($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);

if(empty($result)){
return false;
}
return $result;
}

$localtion = getMobileLoc($r[username]);
print_r($localtion);

猜你喜欢

转载自www.cnblogs.com/songadmin/p/13405257.html