為了記錄用戶的來訪信息,我們需要記錄用戶的位置。在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
)
)