通過IP獲取對應ip的城市信息的函數
/**
* 通過IP獲取對應城市信息(該功能基于淘寶第三方IP庫接口)
* @param $ip IP地址,如果不填寫,則為當前客戶端IP
* @return 如果成功,則返回數組信息,否則返回false
*/
function getIpInfo($ip){
if(empty($ip)) $ip=get_client_ip(); //get_client_ip()為tp自帶函數,如沒有,自己百度搜索。此處就不重復復制了
$url='http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
$result = file_get_contents($url);
$result = json_decode($result,true);
if($result['code']!==0 || !is_array($result['data'])) return false;
return $result['data'];
}
結果例子:
Array
(
[country] => 中國
[country_id] => CN
[area] => 華南
[area_id] => 800000
[region] => 廣東省
[region_id] => 440000
[city] => 深圳市
[city_id] => 440300
[county] =>
[county_id] => -1
[isp] => 電信
[isp_id] => 100017
[ip] => 113.116.200.48
)
有人建議:建議用新浪的接口 淘寶的慢死了,你們怎么看
新聞熱點
疑難解答