腾讯新浪通过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"};


 

使用腾迅的api接口,php获取ip地址以及所在城市

 

http://fw.qq.com/ipaddress返回类似:var IPData = new Array("61.51.71.183","","北京市","");

 

961ddebeb323a10fe0623af514929fc1.jpe 代码
<? 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 );

?>  

代码测试地址:http://www.phpall.cn/forum/ci_data/ip_place.php
该代码须联网使用的,它使用了腾迅的一个api,即http://fw.qq.com/ipaddress
然后用php进行了一些简单的处理,使返回的结果用数组形式显示:
即Array ( [0] => 61.164.140.51 [1] => [2] => 浙江省 [3] => 温州市 )
这样大家就可以很方便的通过数组索引来调用ip地址和所在的省市了。
希望对大家有用。


补充一点:在网上找到另外一种方法:
代码如下:
$ip = $_SERVER['REMOTE_ADDR'];//这里的ip可以是你自定义的ip
$geoInfo = get_meta_tags(http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=$ip);
/*
返回的数组信息如下,不过是英文版本的
Array
(
[known] => true //该IP地址是否可知,即是否有收录;
[locationcode] => CNGDGUAN //地址位置的代码,包含了国家、省份和城市;
[fips104] => CH //美国联邦信息处理标准的国家代码;
[iso2] => CN //iso2标准的国家代码;
[iso3] => CHN //iso标准的国家代码;
[ison] => 156 //用途未明
[internet] => CN //也是国家代码
[countryid] => 49 //国家ID;
[country] => China //国家名称;
[regionid] => 1361 //地区的id,即省份;
[region] => Guangdong //地区名称,即省份名称;
[regioncode] => GD //地区的代码或者缩写;
[adm1code] => CH30 //不清楚其含义;
[cityid] => 3539 //城市的ID;
[city] => Guangzhou //城市的名称;
[latitude] => 23.1170 //纬度;
[longitude] => 113.2500 //经度;
[timezone] => +08:00 //时区;
[certainty] => 78 //不清楚其含义;
)
*/


原文:http://www.phpall.cn/forum/read.php?tid=411

另外封装的一个方法(一个朋友提供):

function  address_baidu( $ip ) {
    
$u   =   "" ;
    
$address   =   file_get_contents ( "  http://open.baidu.com/ipsearch/s?wd={ $ip }&tn=baiduip " );
    
preg_match ( ' #来自:<b>(.+)</b>#Ui ' ,   $address ,   $m );
    
return   strval ( $m [ 1 ]);
}
function  GetRemoteIp( $default = ' 127.0.0.1 ' )
{
    
$ip_string   =   $_SERVER [ ' HTTP_CLIENT_IP ' ] . ' , ' . $_SERVER [ ' HTTP_X_FORWARDED_FOR ' ] . ' , ' . $_SERVER [ ' REMOTE_ADDR ' ];
    
if  (  preg_match  ( " /\d+\.\d+\.\d+\.\d+/ " ,   $ip_string ,   $matches ) )
    {
        
return   $matches [ 0 ];
    }
    
return   $default ;
}
$ip   =  GetRemoteIp();
$addr   =  address_baidu( $ip );


转载请注明本文地址: 腾讯新浪通过IP地址获取当前地理位置(省份)的接口

猜你喜欢

转载自blog.csdn.net/wcqlwyt/article/details/81663769
今日推荐