淘宝获取外网IP php

<?php

private function isRedirectEn()
    {
        if ($this->input->is_ajax_request()) {
            return;
        }
        $this->load->helper('cookie');
        $lang_cookie = get_cookie('lang');
        $ip = $this->input->ip_address();
        // $ip = '47.104.22.91'; // 国内
        // $ip = '47.88.159.132'; // 国外
        $expires_in = 365 * 86400; // cookie 有效时长

        if ($lang_cookie) {
            if ($lang_cookie != 'cn') {
                set_cookie('lang', 'en', $expires_in);
                header("Location: http://www.xxx.com");
                return;
            }
        } else {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_VERBOSE, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
            $http_resp = curl_exec($ch);
            curl_close($ch);
            $res = json_decode($http_resp, true);
            $country_id = $res['data']['country_id'];
            // 是否国内
            $is_inland = in_array($country_id, ['CN', 'xx', '']); // taobao 的接口内网地址会返回xx, 不合法IP会返回空字符串

            if ($is_inland) {
                set_cookie('lang', 'cn', $expires_in);
            } else {
                set_cookie('lang', 'en', $expires_in);
                header("Location: http://www.xxx.com");
                return;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/gaokcl/article/details/81385974