PS:采用IP查询API接口
国内不少互联网公司例如腾讯、新浪以及淘宝都有IP查询接口,直接调用查询即可。
(1)腾讯IP分享计划
代码如下:
/**根据腾讯IP分享计划的地址获取IP所在地,比较精确*/function getIPLoc_QQ($ip1){$url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$ip1;$ch = curl_init($url);curl_setopt($ch,CURLOPT_ENCODING ,'gb2312');curl_setopt($ch, CURLOPT_TIMEOUT, 10);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回$result = curl_exec($ch);$result = mb_convert_encoding($result, "utf-8", "gb2312"); // 编码转换,否则乱码curl_close($ch);preg_match("@<span>(.*)</span></p>@iU",$result,$ipArray);$loc = $ipArray[1];return $loc;}
/**根据腾讯IP分享计划的地址获取IP所在地,比较精确*/function getIPLoc_QQ($ip1){$url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$ip1;$ch = curl_init($url);curl_setopt($ch,CURLOPT_ENCODING ,'gb2312');curl_setopt($ch, CURLOPT_TIMEOUT, 10);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回$result = curl_exec($ch);$result = mb_convert_encoding($result, "utf-8", "gb2312"); // 编码转换,否则乱码curl_close($ch);preg_match("@<span>(.*)</span></p>@iU",$result,$ipArray);$loc = $ipArray[1];return $loc;}
(2)新浪IP查询接口
代码如下:
/**根据新浪IP查询接口获取IP所在地*/function getIPLoc_sina($ip1){$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip1;$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); $loc = "";if($location===FALSE) return "";if (emptyempty($location->desc)) {$loc = $location->province.$location->city.$location->district.$location->isp;}else{$loc = $location->desc;}return $loc;}
/**根据新浪IP查询接口获取IP所在地*/function getIPLoc_sina($ip1){$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip1;$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); $loc = "";if($location===FALSE) return "";if (emptyempty($location->desc)) {$loc = $location->province.$location->city.$location->district.$location->isp;}else{$loc = $location->desc;}return $loc;}
(3)使用淘宝IP接口
代码如下:
/** * 根据淘宝IP查询接口获取IP所在地 */function getCity($ip){$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;$ip=json_decode(file_get_contents($url));if((string)$ip->code=='1'){ return false; } $data = (array)$ip->data;return $data;}
/** * 根据淘宝IP查询接口获取IP所在地 */function getCity($ip){$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;$ip=json_decode(file_get_contents($url));if((string)$ip->code=='1'){ return false; } $data = (array)$ip->data;return $data;}
总结
通过 IP 判断访客来源十分精准的, 现在一些外贸网站都是通过这个方法向用户进行展示区分, 比如美国地区的用户默认看到美国能买到的商品和美国物流信息. 但不是百分之百的准确, 比如某人常年翻墙, 那他可能一直看不到本国的信息. 至于是否需要区分处理, 网站主要有所考虑.
标签:WordPress
相关阅读 >>
wordpress统计当前页面数据库查询次数及消耗时间的方法
解析wordpress中的post_class与get_post_class函数
更多相关阅读请进入《wordpress》频道 >>
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。