echo getCity("221.226.5.1");
function getCity($ip){
//淘宝查询接口
$url = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
//用curl发送接收数据
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING, 'utf8');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$location = curl_exec($ch);
$location = json_decode($location);
curl_close($ch);
return($location->data->city);
}
//stdClass Object ( [code] => 0 [data] => stdClass Object ( [country] => 中国 [country_id] => CN [area] => 华东 [area_id] => 300000 [region] => 江苏省 [region_id] => 320000 [city] => 南京市 [city_id] => 320100 [county] => [county_id] => -1 [isp] => 电信 [isp_id] => 100017 [ip] => 221.226.5.1 ) )
通过淘宝ip查询及remote_addr得到用户城市(脑残版)
最新推荐文章于 2025-02-24 21:43:59 发布
本文介绍了一个简单的PHP函数,用于通过调用淘宝的IP地址查询API获取指定IP地址对应的城市信息。该函数使用curl发送请求并接收返回的数据,最后解析并返回城市的名称。
1万+

被折叠的 条评论
为什么被折叠?



