有时候需要判断IP地址所属地理位置。下面的实例是通过淘宝IP地址库API接口获取IP地址属地。很简单,看代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php /** * 通过淘宝IP接口获取IP地理位置 * @param string $ip * @return: string **/ function getCity($ip) { $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $ipinfo=json_decode(file_get_contents($url)); if($ipinfo->code=='1'){ return false; } $city = $ipinfo->data->region.$ipinfo->data->city; return $city; } // for example print_r(getCity("121.207.247.202")); 福建省福州市 ?> |