当然, php 百度普通ip定位 ,需要注册百度开放平台,然后开发者认证,审核通过后(我是当天就通过了)获得ak,就可以进行开发了,日配额:300万,基本够用的了。下面附上code:
//获取ip地址
function get_ip(){
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else {
$realip = $_SERVER['REMOTE_ADDR'];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")) {
$realip = getenv( "HTTP_X_FORWARDED_FOR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
//百度ip接口
public function get_ip()
{
$ip = get_ip();
$ak = '自己的ak值';
$url = file_get_contents("http://api.map.baidu.com/location/ip?ip=$ip&ak=$ak");
$res1 = json_decode($url,true);
$data =$res1;
if ($data) {
$this->response(array("status" => 1, "msg" => "查询成功", "result" => $data), 'json');
} else {
$this->response(array("status" => -1, "msg" => "查询失败"), 'json');
}
}
打印数据如下:
{ "address": "HK|香港|香港|None|None|0|0", "content": { "address_detail": { "province": "香港特别行政区", "city": "香港特别行政区", "district": "", "street": "", "street_number": "", "city_code": 2912 }, "address": "香港特别行政区", "point": { "y": "2530455.45", "x": "12711279.5" } }, "status": 0 }