为了记录用户的来访信息,我们需要记录用户的位置。在web端,我们可以通过ip来定位来访者的城市。
~~~
/**
* 获取城市
*/
function getCity(){
$ip = request()->ip();
$taobaoUrl = 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $taobaoUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ( $ch, CURLOPT_NOSIGNAL,true);//支持毫秒级别超时设置
curl_setopt($ch, CURLOPT_TIMEOUT, 1200); //1.2秒未获取到信息,视为定位失败
$myCity = curl_exec($ch);
curl_close($ch);
$myCity = json_decode($myCity, true);
return $myCity;
}
~~~
我通过的是 淘宝 的接口。如如下的信息
~~~
Array
(
[code] => 0
[data] => Array
(
[country] => 中国
[country_id] => CN
[area] => 华东
[area_id] => 300000
[region] => 江苏省
[region_id] => 320000
[city] => 南京市
[city_id] => 320100
[county] =>
[county_id] => -1
[isp] => 电信
[isp_id] => 100017
[ip] => 114.222.66.172
)
)