需求
找出用户ip归属地
解决
大多接口需要收费,但是通过抓包我们还是能从一些查询网站中,找到他们的接口来供自己调用
// ip.cn
public static function ip_cn($ip){
$url ='https://ip.cn/api/index?type=1&ip='.$ip;
$result = Curl::httpGet($url);
$arr = json_decode($result,true);
print_r($arr);;
}
// 百度接口
public static function ip_baidu($ip){
$t = time().rand(100,666);
$et = time().rand(666,999);
$statime = (time()+5).rand(100,999);
$url ='https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query='.$ip.'&co=&resource_id=6006&t='.$statime.'&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110207'.$et.'_'.$t.'&_='.$et;
$result = Curl::httpGet($url);
$return_data = mb_convert_encoding($result, "UTF-8","GBK");
$city_str = '';
$city = array();
preg_match_all("/(?:\()(.*)(?:\))/i",$return_data, $city);
if(!empty($city[1][0])){
$ipstr = $city[1][0];
$ipdata = json_encode($ipstr);
$ipdata = json_decode( json_decode($ipdata),true );
if(!empty($ipdata['data'][0]['location'])){
Log::syslog("百度接口:".$ipdata['data'][0]['location'], "get_h5url");
$city_str = $ipdata['data'][0]['location'];
}
}
return $city_str;
}
// ip168 接口
public static function ip_168($ip){
$post_data['keyword'] = $ip;
$post_data['btnsearch'] = '查询';
$url ='http://www.ip168.com/chxip/doGetIp.do';
$result = Curl::httpPost($url,$post_data);
$city_str = '';
if(!empty($result)){
Log::syslog("ip168接口:".$result, "get_h5url");
$city_str = $result;
}
return $city_str;
}
// 淘宝接口
public static function ip_taobao($ip){
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
$result = Curl::httpGet($url);
$ip = json_decode($result,true);
$city_str = '';
if(!empty($ip['data']['city'])){
Log::syslog("淘宝接口:".$ip['data']['city'], "get_h5url");
$city_str = $ip['data']['city'];
}
return $city_str;
}