• IP库笔记


    ipip.net

    https://github.com/ipipdotnet/ipdb-php

    代码实现,依赖IP库文件,可以去官网下载免费版测试

    需要按照依赖的库 composer require ipip/db

        public static function ipNet($ip)
        {
            try {
                static $city = null;
                if (empty($city)) {
                    $dbFile = config_path("data/ipipfree.ipdb");
                    $city = new ipipdbCity($dbFile);
                }
                return $city->findInfo($ip, 'CN');
            } catch (Exception $ex) {
                $msg = "by ipip.net getLocalCountry {$ip} fail {$ex->getMessage()}";
                Log::error($msg);
                throw new Exception($msg);
            }
    
        }
    

    结果如下:

    ipipdbCityInfo Object
    (
        [country_name] => 法国
        [region_name] => 法国
        [city_name] => 
        [owner_domain] => 
        [isp_domain] => 
        [latitude] => 
        [longitude] => 
        [timezone] => 
        [utc_offset] => 
        [china_admin_code] => 
        [idd_code] => 
        [country_code] => 
        [continent_code] => 
        [idc] => 
        [base_station] => 
        [country_code3] => 
        [european_union] => 
        [currency_code] => 
        [currency_name] => 
        [anycast] => 
    )
    

    包括运营商等其它信息的可以购买收费版本

    淘宝IP库

    http://ip.taobao.com/

    纯真 IP 库、淘宝 IP 库、腾讯 IP 库、新浪 IP 库

    为了保障服务正常运行,每个用户的访问频率需小于1qps

    代码实现(依赖Curl库):

     public static function ipNetTaobao($ip)
        {
            try {
                $opt = [
                    CURLOPT_TIMEOUT => 5,
                    CURLOPT_RETURNTRANSFER => true,
                ];
                $result = json_decode(Curl::get('http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip, $opt), true);
                if ($result && $result['code'] === 0) {
                    return $result['data'];
                } else {
                    throw new Exception('fail ');
                }
            } catch (Exception $ex) {
                $msg = "by taobao getLocalCountry {$ip} fail {$ex->getMessage()}";
                Log::error($msg);
                throw new Exception($msg);
            }
    
        }
    

    结果如下:

    Array
    (
        [ip] => 223.104.64.66
        [country] => 中国
        [area] => 
        [region] => 广东
        [city] => 汕头
        [county] => XX
        [isp] => 移动
        [country_id] => CN
        [area_id] => 
        [region_id] => 440000
        [city_id] => 440500
        [county_id] => xx
        [isp_id] => 100025
    )
    

    自己的IP库设计

    country 中国 国家
    area 华北 区域
    region 广东 地区
    city 汕头 城市

    数据库表:

    CREATE TABLE `base_ip_store` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `ip_start` varchar(15) NOT NULL,
      `ip_end` varchar(15) NOT NULL,
      `long_ip_start` bigint(21) NOT NULL,
      `long_ip_end` bigint(21) NOT NULL,
      `country_code` varchar(20) DEFAULT NULL COMMENT '国家代码:中国CN,美国USA',
      `country_id` int(11) DEFAULT '0' COMMENT '国家id',
      `country_name` varchar(50) DEFAULT NULL COMMENT '国家名称',
      `region_id` int(11) DEFAULT '0' COMMENT '省/州id',
      `region_name` varchar(50) DEFAULT NULL COMMENT '地区名称,比如:湖南省',
      `area_id` int(11) DEFAULT '0' COMMENT '区域ID',
      `area_name` varchar(50) DEFAULT NULL COMMENT '区域名称,比如华北,东北',
      `city_id` int(11) DEFAULT '0' COMMENT '城市ID',
      `city_name` varchar(50) DEFAULT NULL COMMENT '城市名称比如 北京,东京',
      `isp_id` int(11) DEFAULT '0' COMMENT '供应商ID ',
      `isp` varchar(50) DEFAULT NULL COMMENT '供应商名称',
      `county_id` int(11) DEFAULT '0' COMMENT '郡/县',
      `county_name` varchar(50) DEFAULT NULL COMMENT '郡/县名称',
      `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
      `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`),
     KEY `idx_strore_ip_start_end` (`ip_start`,`ip_end`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COMMENT='基础IP库'
    
    
    

    注意入库的时候,程序在32位下转成长ip可能会出现负数,处理如下:

    $long_ip =sprintf("%u", ip2long($ip));

  • 相关阅读:
    MySQL数据模型
    Spring循环依赖
    @Autowired和@Resource区别
    Kafka概念
    阻塞队列
    线程池原理
    Spring AOP
    JVM 史上最最最完整深入解析(12000 字噢)
    Dubbo配置信息
    友情链接
  • 原文地址:https://www.cnblogs.com/zqsb/p/11890992.html
Copyright © 2020-2023  润新知