在團購、門戶網站都有顯示用戶所在城市信息的功能。究竟是怎麼實現的呢?原理很簡單,就是根據用戶ip,在數據庫裏查找它是哪兒的ip,顯示就完了。問題的關鍵是ip庫好不好用,之前下了一個,奇大無比,效果實在不怎麼樣。今天發現了一個騰訊的接口,它的數據庫對
們的應用來說夠用了。下面貼代碼
騰訊的接口是 ,返回數組 http://fw.qq.com/ipaddress
返回值 var IPData = new Array("61.135.152.194","","北京市","");
function get_ip_place()
{
$ip=file_get_contents("http://fw.qq.com/ipaddress");
$ip=str_replace('"',' ',$ip);
$ip2=explode("(",$ip);
$a=substr($ip2[1],0,-2);
$b=explode(",",$a);
return $b;
}
$ip=get_ip_place();
print_r($ip);