apolloren
<?php
 
//获取用户ip(外网ip 服务器上可以获取用户外网Ip 本机ip地址只能获取127.0.0.1)
function getip(){
    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
    $cip = $_SERVER["HTTP_CLIENT_IP"];
    }
    else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
    $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    }
    else if(!empty($_SERVER["REMOTE_ADDR"])){
    $cip = $_SERVER["REMOTE_ADDR"];
    }
    else{
    $cip = \'\';
    }
    preg_match("/[\d\.]{7,15}/", $cip, $cips);
    $cip = isset($cips[0]) ? $cips[0] : \'unknown\';
    unset($cips);
    return $cip;
}
 
$ip=getip();
 
//根据百度地图api得到用户Ip地理经纬度和城市
 
$url = "http://api.map.baidu.com/location/ip?ak=omi69HPHpl5luMtrjFzXn9df&ip=$ip&coor=bd09ll";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
if(curl_errno($ch))
{ echo \'CURL ERROR Code: \'.curl_errno($ch).\', reason: \'.curl_error($ch);}
curl_close($ch);
$info = json_decode($output, true);
if($info[\'status\'] == "0"){
    $lotx = $info[\'content\'][\'point\'][\'y\'];
    $loty = $info[\'content\'][\'point\'][\'x\'];
    $citytemp = $info[\'content\'][\'address_detail\'][\'city\'];
    $keywords = explode("市",$citytemp);
    $city = $keywords[0];
}
else{
    $lotx = "34.2597";
    $loty = "108.9471";
    $city = "西安";
}
 
 
var_dump($lotx);//x坐标  纬度
var_dump($loty);//y坐标  经度
var_dump($city);//用户Ip所在城市
  

  

 --------------------------------------------------

百度地图api新增功能  有时间可以看看

http://developer.baidu.com/map/index.php?title=jspopular

分类:

技术点:

相关文章:

  • 2021-11-14
  • 2021-12-25
  • 2021-10-01
  • 2021-11-08
  • 2022-01-22
  • 2021-12-04
  • 2021-12-06
  • 2021-09-21
猜你喜欢
  • 2021-12-16
  • 2021-10-01
  • 2021-11-11
  • 2021-11-17
  • 2021-10-27
  • 2022-01-01
相关资源
相似解决方案