网站首页 文章专栏 根据IP获取所在地的省份
/**
* 通过访问新浪接口获取IP所在地
* @DateTime: 2018-01-03 16:44:22
*
*/
function getIPLoc_sina($queryIP) {
$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=' . $queryIP;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
$location = curl_exec($ch);
$location = json_decode($location);
curl_close($ch);
$loc = "";
if ($location === FALSE) {
return "";
}
if (!$location->desc) {
$loc = $location->province;
} else {
$loc = $location->desc;
}
return $loc;
}
/**
* 淘宝接口获得省份名称
* @DateTime: 2018-06-11 19:42:28
*
*/
function getIPLoc_taobao($queryIP) {
$url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $queryIP;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
$location = curl_exec($ch);
$location = json_decode($location);
curl_close($ch);
$arr = object_array($location);
if ($arr['code'] == 1) {
return false;
}
return $arr['data']['region'];
}
转载请注明出处