PHP 获取 IP 地址 函数封装

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_34248133/article/details/102505624
/**
 * 获取 IP 的地理位置
 *
 * @param  string     $ip   客户端ip
 * @return array
 */
function get_ip_address($ip)
{
    $arr = [];
    /**
     * 全球五大区域性因特网注册管理机构:
     * 1. ARIN    主要负责北美地区业务
     * 2. RIPE    主要负责欧洲地区业务
     * 3. LACNIC  主要负责拉丁美洲美洲业务
     * 4. AfriNIC 负责非洲地区业务
     * 5. APNIC   管理亚太地区国家的IP地址和AS号码
     * @var array
     */
    $arr1 = ['ARIN','RIPE','APNIC','LACNIC','AfriNIC'];

    // 判断是否为内网ip
    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) || $ip == '127.0.0.1') {
        // 识别本地公网ip
        $ip = curl_get_https('https://ifconfig.me/ip');
    }


    // 百度API
    $url1 = 'https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query='.$ip.'&co=&resource_id=6006&t=1488163851795&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery1102007553074611919763_1488163568327&_=1488163568331';
    $html = '';
    $html = curl_get_https($url1); // 拿数据
    $html = iconv('GB2312', 'UTF-8//IGNORE', $html); // 将字符串的编码从GB2312转到UTF-8
    $str2 = cut_str($html,'"',17); // 取地址

    // 如果为匿名ip,则调取ipip.net的api
    if (in_array($str2, $arr1)) {
        // 拼接字符串
        $url = 'http://freeapi.ipip.net/' . $ip;
        $html = curl_get_https($url);
        $str2 =
        (empty(cut_str($html,'"',1)) ? '' : cut_str($html,'"',1)) .
        (empty(cut_str($html,'"',3)) ? '' : ' - ' . cut_str($html,'"',3)) .
        (empty(cut_str($html,'"',5)) ? '' : ' - ' . cut_str($html,'"',5)) .
        (empty(cut_str($html,'"',7)) ? '' : ' - ' . cut_str($html,'"',7)) .
        (empty(cut_str($html,'"',9)) ? '' : ' - ' . cut_str($html,'"',9)); // 取地址
    }

    $arr['ip']      = $ip;
    $arr['address'] = $str2;

    return $arr;
}

function cut_str($str, $sign, $number){
    $array  = explode($sign, $str);
    $length = count($array);
    if($number < 0){
        $new_array  = array_reverse($array);
        $abs_number = abs($number);
        if($abs_number > $length){
            return 'error';
        }
        else{
            return $new_array[$abs_number-1];
        }
    }
    else{
        if($number >= $length){
            return 'error';
        }
        else{
            return $array[$number];
        }
    }
}

function curl_get_https($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
    $res = curl_exec($ch);
    curl_close($ch);
    return $res;
}

另外, 在用的时候遇到了一个问题:

curl_setopt(): CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead:

解决方法是, 加上下面的: (我以前是没有加这个的, 后来查了一下, 好像curl版本问题, 果断改了, 改完后ok)

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

附curl版本:

猜你喜欢

转载自blog.csdn.net/qq_34248133/article/details/102505624
今日推荐