This article mainly shares with you the code for obtaining the IP address location in PHP. I hope it can help you.
/** * 获取IP地址所在地 */ function getIPLoc($ip) { $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip; $ch = curl_init($url); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回 $result = curl_exec($ch); curl_close($ch); // $result = httpRequest($url); $result = json_decode($result); $data = array(); if ($result && !empty($result->province)) { $data['country'] = $result->country; $data['province'] = $result->province; $data['city'] = $result->city; } return $data; }
Related recommendations:
php implementation of obtaining the geographical location of the IP address
php obtains the IP address location query program_PHP Tutorial
Based on PHP to obtain the IP address through photos, _PHP Tutorial
The above is the detailed content of PHP implements code sharing to obtain IP address location. For more information, please follow other related articles on the PHP Chinese website!