获取IP地址以及判断ip是否是云服务器的

一,获取ip地址

1, 淘宝ip不稳定,放弃
/**
     * 淘宝接口不稳定,放弃 {"msg":"the request over max qps for user ,the accessKey=public","code":4}
     * http://ip.taobao.com/service/getIpInfo.php?ip=220.181.38.148
     * ip转地区,接口:淘宝ip
     * @param string $ip
     * @param bool $raw 返回全部信息
     * @return mixed|string
     */
   public function get_region_taobao_ip($ip='8.8.8.8', $raw = false)
   {
       if (!$ip) {
           return '未知';
       }
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       // curl_setopt($ch, CURLOPT_VERBOSE, true);// 详细命令
       curl_setopt($ch, CURLOPT_TIMEOUT, 1);
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
       $http_resp = curl_exec($ch);
       curl_close($ch);
       $res = json_decode($http_resp, true);
       if ($raw) {
           return $res;
       }
       $country_id = $res['data']['country_id'];
       // 是否有效
       $is_valid = !in_array($country_id, ['xx', '']); // taobao 的接口内网地址会返回xx, 不合法IP会返回空字符串
       if ($is_valid) {
           if ($country_id != 'CN') {return '国外';}
           return $res['data']['country'] . ' ' . $res['data']['region'] . ' ' . $res['data']['city'];
       } else {
           return '未知';
       }
   }
2, 仿真ip 与 百度ip
    /**
     * 1,仿真ip:https://github.com/itbdw/ip-database
     * 先使用仿真ip获取,获取不到使用百度ip
     * ip转地区,接口:百度ip
     * 2,百度ip:http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=220.181.38.148&co=&resource_id=6006
     * @param string $ip
     * @return string
     */
    private function get_region_ip($ip = '8.8.8.8')
    {
        if (empty($ip)) {
            return '未知';
        }

        /**
         * 仿真ip
         */
        $ip_info = json_decode(json_encode(IpLocation::getLocation($ip), JSON_UNESCAPED_UNICODE), true);
        if (isset($ip_info['error']) !== true) {
            return !empty($ip_info['area']) ? trim($ip_info['area']) : '未知';
        }

        /**
         * 百度ip
         */
        $api = "http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=" . $ip . "&co=&resource_id=6006";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        $handles = curl_exec($ch);
        curl_close($ch);
        $handles = iconv('GB2312', 'UTF-8', $handles);
        $arr = json_decode($handles, true);
        $location = !empty($arr['data'][0]['location']) ? $arr['data'][0]['location'] : '未知';

        return $location;
    }
3, 排除IP地址含“云”的地址(待补充所有地名,排除云服务器访问)
    /**
     * 判断是否是云服务器(ip访问来自云服务器)
     * github: https://github.com/modood/Administrative-divisions-of-China
     * 中华人民共和国行政区划(五级):省级、地级、县级、乡级和村级。【这里到县级】
     * @param string $ip_area ip地址
     * @return bool true-是 false-否
     */
    public function judge_yun_ip($ip_area = '')
    {
        // 排除地名中含有以下的地名
        $remove_dict = ['云南', '连云港', '云浮市', '云林县', '白云区', '云龙县', '云冈区', '云州区', '左云县', '白云鄂博矿区', '云龙区', '连云区', '灌云县', '缙云县', '云和县', '云霄县', '青云谱区', '庆云县', '云梦县', '云龙示范区', '云溪区', '云城区', '云安区', '凌云县', '云阳县', '云岩区', '紫云苗族布依族自治县', '云县', '祥云县', '云龙县'];
        foreach ($remove_dict as $val) {
            if (strpos($ip_area, $val) !== false) {
                $ip_area = str_replace($val, '', $ip_area);
            }
        }
        // 疑似云服务器访问
        if (strpos($ip_area, '云') !== false) {
            return true;
        }
        return false;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值