在直接在ie浏览器中打开下面地址
http://ip.taobao.com/service/getipinfo.php?ip=8.8.8.8
返回信息
代码如下 复制代码
{code:0,data:{country:u7f8eu56fd,country_id:us,area:,area_id:,region:,region_id:,city:,city_id:,county:,county_id:,isp:,isp_id:,ip:8.8.8.8}}
上面我们是直接在ie地址输入,这里我们利用php file_get_contents函数来获取
代码如下 复制代码
/**
* 获取 ip 地理位置
* 淘宝ip接口
* @return: array
*/
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;
}
上面原因是通过file_get_contents由淘宝网返回的json格式的数据,我们再利用php json_decode函数转换成数组。
$ip地址需要我们提供,下面提供一个获取用户真实ip地址的函数
代码如下 复制代码
function getip()
{
static $realip;
if (isset($_server)){
if (isset($_server[http_x_forwarded_for])){
$realip = $_server[http_x_forwarded_for];
} else if (isset($_server[http_client_ip])) {
$realip = $_server[http_client_ip];
} else {
$realip = $_server[remote_addr];
}
} else {
if (getenv(http_x_forwarded_for)){
$realip = getenv(http_x_forwarded_for);
} else if (getenv(http_client_ip)) {
$realip = getenv(http_client_ip);
} else {
$realip = getenv(remote_addr);
}
}
return $realip;
}
http://www.bkjia.com/phpjc/631546.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/631546.htmltecharticle我们有时不想利用自己的数据库存储ip地址,自己的ip库更新慢不及时,我们可以直接使用第三方的ip库来操作,这里介绍利用淘宝ip数据获取...
