现在好多ip地址库已经不能用了,现有的接口要么收费(淘宝、腾讯、新浪、ip138等),要么查询不准(126),现在开源一个百度公开的ip查询api
/*
* 百度ip地址查询接口powby:qq:1620016116;
*/
function ipToArea($ip=""){
$api="http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=".$ip."&co=&resource_id=6006";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$api);
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_HTTPHEADER,C('IP138_TOKEN'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,3);
$handles = curl_exec($ch);
curl_close($ch);
$handles= iconv('GB2312', 'UTF-8', $handles);
$arr=json_decode($handles,true);
return $arr[data][0][location];
}