您好,欢迎访问一九零五行业门户网

php使用IP匹配城市信息的方法

本文主要和大家分享php使用ip匹配城市信息的方法,希望能帮助到大家。
1、首先需要获取用户的真实ip
function ip(){ if(getenv('http_client_ip') && strcasecmp(getenv('http_client_ip'), 'unknown')){ $ip = getenv('http_client_ip'); }elseif(getenv('http_x_forwarded_for') && strcasecmp(getenv('http_x_forwarded_for'), 'unknown')){ $ip = getenv('http_x_forwarded_for'); }elseif(getenv('remote_addr') && strcasecmp(getenv('remote_addr'), 'unknown')){ $ip = getenv('remote_addr'); }elseif(isset($_server['remote_addr']) && $_server['remote_addr'] && strcasecmp($_server['remote_addr'], 'unknown')){ $ip = $_server['remote_addr']; } return preg_match("/[\d\.]{7,15}/", $ip, $matches) ? $matches[0] : 'unknown'; }
2、使用第三方接口匹配城市
注:
匹配的城市信息需要存储到数据库,方便后期使用
第三方提供的api接口,目前网易和新浪的较稳定易用
1)新浪ip地址查询api接口
此处显示返回json格式数据
接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=ip地址
示例代码:片段
$ip = ip();$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip;$res = file_get_contents($url);echo $res;die;
结果如图:
2)网易有道ip地址api接口
接口:http://www.youdao.com/smartresult-xml/search.s?type=ip&q=ip地址
结果如图:
3)淘宝ip地址库api接口
接口:http://ip.taobao.com/service/getipinfo.php?ip=[ip地址字串]
示例代码:
$ip = ip();$url = 'http://ip.taobao.com/service/getipinfo.php?ip='.$ip;$res = file_get_contents($url);echo $res;die;
结果如图:
3、其他ip地址查询接口及调用方法
设计蜂巢ip地址查询接口:http://www.hujuntao.com/api/ip/ip.php
腾讯ip地址查询接口:http://fw.qq.com/ipaddress
新浪ip地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
搜狐ip地址查询接口:http://pv.sohu.com/cityjson
谷歌ip地址查询接口:http://j.maxmind.com/app/geoip.js
有道ip地址查询接口:http://www.youdao.com/smartresult-xml/search.s
1616 ip地址查询接口:http://w.1616.net/chaxun/iptolocal.php
126 http://ip.ws.126.net/ipquery
hao123 http://app.hao123.com/ipquery/getcity.php?rtype=2
淘宝 http://ip.taobao.com/service/getipinfo.php?ip=117.89.35.58
太平洋电脑网 http://whois.pconline.com.cn/
相关推荐:
php判断ip匹配
以上就是php使用ip匹配城市信息的方法的详细内容。
其它类似信息

推荐信息