// 获取ip
function ip()
{
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if (isset($_SERVER["HTTP_CLIENT_IP"]))
$ip = $_SERVER["HTTP_CLIENT_IP"];
else
$ip = $_SERVER["REMOTE_ADDR"];
return $ip;
}
$ip = ip();
// 通过百度apistore中api获取
// http://apistore.baidu.com/apiworks/servicedetail/114.html
$ch = curl_init();
$url = "http://apis.baidu.com/apistore/iplookupservice/iplookup?ip=$ip";
$header = array(
'apikey: 你的apikey',
);
// 添加apikey到header
curl_setopt($ch, CURLOPT_HTTPHEADER , $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行HTTP请求
curl_setopt($ch , CURLOPT_URL , $url);
$res = curl_exec($ch);
$arr = json_decode($res, true);
echo "ip: " . $arr['retData']['ip'] . "
";
echo "address: " . $arr['retData']['country'] . "-" . $arr['retData']['province'] . "-" . $arr['retData']['city'] . "-" . $arr['retData']['district'] . "-" . $arr['retData']['carrier'];