二:根据百度地图提供的接口地址获取实际地址信息
这里以普通IP定位为例:
三,在php中的代码
function getIpAddress($ip){
$data_info=curl("http://api.map.baidu.com/location/ip?ip=".$ip."&ak=xxxxxxxxxxxxx");
$data_info = json_decode($data_info,true); //数据转换
if(!isset($data_info['content'])){
$data_info['content']['address']='内网地址127.0.0.1';
}
return $data_info['content']['address'];
}
function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//参数为1表示传输数据,为0表示直接输出显示。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //参数为0表示不带头文件,为1表示带头文件
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
第二种格式:
function get_client_dizhi($ip){
$url = 'https://api.map.baidu.com/location/ip?ak=3mOsomnzOmq0bsOqPGgx390MsHpWNfra&coor=bd09ll&ip='.$ip;
$ch = curl_init();
if(substr($url,0,5)=='https'){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
// $a='{"address":"CN|\u6e56\u5357\u7701|\u5cb3\u9633\u5e02|None|None|100|100","content":{"address":"\u6e56\u5357\u7701\u5cb3\u9633\u5e02","address_detail":{"adcode":"430626","city":"\u5cb3\u9633\u5e02","city_code":"220","district":"","province":"\u6e56\u5357\u7701","street":"","street_number":""},"point":{"x":"113.13548942","y":"29.36317829"}},"status":0}';
$resArr=json_decode($response,true);
if($resArr['status']!=0){
return $resArr['message'];
}
// return $resArr;
return $resArr['content']['address'];
}
/*