例如 URL:http://ipinfodb.com/ip_locator.php?ip=8.8.8.8返回:"City, State" if found otherwise defaults to "Hollywood, CA"
[JavaScript]代码
function detect_city($ip) { $default = 'Hollywood, CA'; if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost') $ip = '8.8.8.8'; $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)'; $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip); $ch = curl_init(); $curl_opt = array( CURLOPT_FOLLOWLOCATION => 1, CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_USERAGENT => $curlopt_useragent, CURLOPT_URL => $url, CURLOPT_TIMEOUT => 1, CURLOPT_REFERER => 'http://' . $_SERVER['HTTP_HOST'], ); curl_setopt_array($ch, $curl_opt); $content = curl_exec($ch); if (!is_null($curl_info)) { $curl_info = curl_getinfo($ch); } curl_close($ch); if ( preg_match('{<li>City : ([^<]*)</li>}i', $content, $regs) ) { $city = $regs[1]; } if ( preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs) ) { $state = $regs[1]; } if( $city!='' && $state!='' ){ $location = $city . ', ' . $state; return $location; }else{ return $default; } }
收藏
0人收藏
- 2014-12-16 09:22:19java获得真实IP代码 by 金背二郎
- 2012-11-13 14:01:55php查询纯真IP数据库获得IP的地理位置 by 0晓风残月0
- 2013-03-26 13:14:10Perl 查询域名对应的 IP 地址 by 他山之石
- 2014-09-10 09:16:03python根据域名获得ip地址 by jack.chen
- 2013-03-26 18:35:26获取 iOS 设备当前 ip 地址 by 冬夜微风
- 2014-10-05 14:08:43python ip和long类型之间的相互转换 by 甄码农
- 2012-11-24 19:21:50配置IP地址的shell脚本 by zetaliang
- 2013-03-31 18:01:20php调用腾讯API显示你当前所在城市 by Sycro
- 2014-10-28 21:32:56Delphi版IP地址与整型互转 by p5soft
- 2012-11-27 10:57:04c#获得服务器的外网ip,内网ip by 西国の海妖
- 2018-12-26 11:42:30java B2B2C springmvc mybatis仿淘宝电子商城系统-整合企业架构的技术点 by it绿萝
相关聚客文章
- 转:将图片保存在iPhone的相册中
- PHP抓取TP-Link页面获取外网IP脚本
- Apple's Contracts With European iPhone Carriers Ex
- Linux双网卡双IP、网关的设置
- RHEL/CentOS修改ip_conntrack的HASHSIZE
- windows2003 DHCP中批处理绑定IP与MAC
- [翻译]在Ubuntu 12.04上利用IP Tables来设置防火墙
- python访问纯真IP数据库
- 为服务器IP添加反向DNS解析
- 机房网段更改后,如何避免在机房逐台更改IP?
- 10.2.0.1 RAC中重建CRS的过程(比如错误的修改IP导致CRS不能启动)
- 让Apache拒绝网络流氓蜘蛛