根据IP获取国家省市

< ?php     

function get_ip_place(){     

$ip=file_get_contents("http://fw.qq.com/ipaddress");     

$ip=str_replace('"',' ',$ip);     

$ip2=explode("(",$ip);     

$a=substr($ip2[1],0,-2);     

$b=explode(",",$a);     

return $b;     

}     

$ip=get_ip_place();     

print_r($ip); ?> 

 

代码须联网使用的,使用腾迅的一个api,即http://fw.qq.com/ipaddress

然后用php进行了简单的处理,使返回的结果用数组形式显示:

即Array ( [0] =>123.5.115.119 [1] => [2] => 河南省 [3] => 新乡市)

 

ps:同理用JS也可以通过腾迅的API获得IP地址和IP地址所在的地区。

 

 

腾讯的接口是:http://fw.qq.com/ipaddress 

返回数组,其返回值: 

var IPData = new Array("61.135.152.194","","北京市",""); 

 

新浪的接口是:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 

多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?

 

format=js&ip=218.192.3.42 

返回值: 

var remote_ip_info =

 

{"ret":1,"start":"218.192.0.0","end":"218.192.7.255","country":"\u4e2d\u56fd","province":

 

"\u5e7f\u4e1c","city":"\u5e7f\u5dde","district":"","isp":"\u6559\u80b2

 

\u7f51","type":"\u5b66\u6821","desc":"\u5e7f\u5dde\u5927\u5b66\u7eba\u7ec7\u670d\u88c5

 

\u5b66\u9662"};

 

 

通过jquery 获取相应的数据 

 

复制代码 代码如下: 

$.getScript('数据接口',function(){ 

//腾讯:IPData[0]  

 //新浪:remote_ip_info.country 

... 

}) 

 

 

 

AS3代码:

 

System.useCodePage=true;

var url:URLRequest=new URLRequest();

url.url="http://fw.qq.com/ipaddress";

var reg:RegExp=/".*"/ig;

var reg2:RegExp=/"/ig;

var urlLoad:URLLoader=new URLLoader();

urlLoad.addEventListener(Event.COMPLETE,onComplete);

urlLoad.load(url);

function onComplete(ev:Event) {

 var str:String=ev.target.data;

 var getStr:String;

 var datas:Array=str.match(reg);

 trace("str:"+str);

 if (datas) {

  getStr=datas[0].replace(reg2,"");

  datas=getStr.split(",");

  trace(datas[0]+" "+datas[2]+" "+datas[3]);  

 }

}
 

新浪可用,腾讯貌似挂了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值