php获取ip归属地

通过用户登录的IP地址来获取用户当前的归属地,也就是用户现在在哪个省份
方法一、

<?php
//太平洋网络查询ip,较快
     $ip = "182.149.164.150";
     $get_ip_city = get_ip_city($ip);
     var_dump($get_ip_city);
 
 
     /**
      *$ip  string  必传
      *获取ip归属地 
      *demo 四川省成都市 电信 
      */
     function get_ip_city($ip){     	
	     $ch = curl_init();
	     $url = 'https://whois.pconline.com.cn/ipJson.jsp?ip='.$ip;
	     //用curl发送接收数据
	     curl_setopt($ch, CURLOPT_URL, $url);
	     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	     //请求为https
	     curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
	     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	     $location = curl_exec($ch);
	     curl_close($ch);
	     //转码
	     $location = mb_convert_encoding($location, 'utf-8','GB2312');
	     //var_dump($location);
	     //截取{}中的字符串
	     $location = substr($location, strlen('({')+strpos($location, '({'),(strlen($location) - strpos($location, '})'))*(-1));
	    //将截取的字符串$location中的‘,’替换成‘&’   将字符串中的‘:‘替换成‘=’
	     $location = str_replace('"',"",str_replace(":","=",str_replace(",","&",$location)));
	     //php内置函数,将处理成类似于url参数的格式的字符串  转换成数组
	     parse_str($location,$ip_location);
	     return $ip_location['addr'];
     }

方法二、

 
<?php
//淘宝接口慢
function get_ip_city($ip)
{
    $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;//淘宝  string(12) "四川成都"
    $ipinfo=json_decode(file_get_contents($url));
    if($ipinfo->code=='1'){
        return false;
    }
    $location = $ipinfo->data->region.$ipinfo->data->city.$ipinfo->data->isp;
    return $location;
}
 
$ip="182.149.164.150" ;
var_dump(get_ip_city($ip));  //string(18) "四川成都电信"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java可以通过调用第三方IP库来实现通过IP地址获取IP归属地的功能。下面是一个使用淘宝IP库的示例代码: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import com.alibaba.fastjson.JSONObject; public class IpUtil { /** * 通过 IP 地址获取地址信息 * * @param ip IP 地址,例如:192.168.0.1 * @return 地址信息,例如:国|0|北京|北京市|联通 */ public static String getAddressInfo(String ip) { String result = ""; try { // 淘宝IP库API接口 String apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip; URL url = new URL(apiurl); URLConnection conn = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { result += line; } in.close(); } catch (Exception e) { System.out.println("Error in getting IP address information: " + e.getMessage()); } return result; } public static void main(String[] args) { String addressInfo = getAddressInfo("112.80.248.75"); JSONObject jsonObject = JSONObject.parseObject(addressInfo); if (jsonObject.getIntValue("code") == 0) { JSONObject data = jsonObject.getJSONObject("data"); System.out.println(data.getString("country")); System.out.println(data.getString("region")); System.out.println(data.getString("city")); } else { System.out.println("Error in getting IP address information: " + jsonObject.getString("msg")); } } } ``` 上述代码的`getAddressInfo`方法使用了淘宝IP库的API接口来获取IP地址归属地信息。其,`jsonObject.getIntValue("code") == 0`表示查询成功,`data`是一个JSON对象,里面包含了`country`、`region`和`city`等地址信息字段。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

于先生吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值