私人领地

thikphp5.0 ip地址库 解决卡顿问题 curl_init

使用淘宝新浪的地址库非常的使用,但是调用有时候会出现很慢。会导致卡在当前网页。

 

要想不影响当前速度,因此要使用 curl_init功能。

项目案例:会员登陆日志  

user_log 字段:id,user_id,user_name,ip,address,add_time

思路:

1.如果登陆成功send线下发送请求,带参数id,name,ip

2.在方法add_log接收参数,并且开始转化值(要插入user_log字段的数据),其中会用到getAddressFromIp 淘宝新浪地址库

 

 

具体操作: 

第一步骤:如果登录成功,就开始发送异步请求

$res = db()->where()->update();
if
($res){ // 创建session数据 $user['id'] = $login['data']['id']; $user['name'] = $login['data']['name']; $user['role_name'] = model('role')->RoleText($login['data']['role_id']); $user['action_list'] = $login['data']['action_list']; $user['token'] = $token; Session::set('user',$user); // 新增登陆日志 $http = [ 'id'=>Session::get('user.id'), 'name' => Session::get('user.name'), 'ip' => request()->ip(), ]; send("http://".request()->host().url('login/add_log',$http)); //send('http://localhost/rookie/admin/login/add_log.html'); // 跳转内页 $this->redirect('index/index'); }else{ $this->error('登陆失败'); }

 

第二步骤,要准备写几个方法,方便后面调用

/**
 * php 异步请求数据
 * @host 格式
 * @http://localhost/rookie/login/add.html
 * @http://www.xxx.com
 * */
function send($host){
    // 创建一个新cURL资源
    $ch = curl_init();

    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, $host);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // 抓取URL并把它传递给浏览器
    curl_exec($ch);
    // 关闭cURL资源,并且释放系统资源
    curl_close($ch);
}

 

/**
     * 新增登陆日志
     * @ get传递过来参数  id,name,ip
     * 格式:http://localhost/rookie/admin/login/add_log.html?id=1&name=admin&ip=113.102.134.153
     * @ 需要合成 id,name,ip,address,add_time
     * */
    public function add_log(){
        //$ip = '113.102.134.153';
        $user_id = input('param.id');
        $user_name = input('param.name');
        $ip = input('param.ip');

        $data['user_id'] = $user_id;
        $data['user_name'] = $user_name;
        $data['ip'] = $ip;
        $address = getAddressFromIp($ip);
        $data['address']  = implode(' ',$address);
        $data['add_time'] = time();

        db('user_login')->insert($data);

        //file_put_contents('7.txt',urldecode(json_encode($data)));
        //file_put_contents('7.txt',$user_name);
    }

 

/*
 *  新浪,淘宝IP地址库
 * */
function getAddressFromIp($ip){
    $urlTaobao = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
    $urlSina = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip;
    $json = file_get_contents($urlTaobao);
    $jsonDecode = json_decode($json);
    if($jsonDecode->code==0){//如果取不到就去取新浪的
        $data['country'] = $jsonDecode->data->country;
        $data['province'] = $jsonDecode->data->region;
        $data['city'] = $jsonDecode->data->city;
        $data['isp'] = $jsonDecode->data->isp;
        return $data;
    }else{
        $json = file_get_contents($urlSina);
        $jsonDecode = json_decode($json);
        $data['country'] = $jsonDecode->country;
        $data['province'] = $jsonDecode->province;
        $data['city'] = $jsonDecode->city;
        $data['isp'] = $jsonDecode->isp;
        $data['district'] = $jsonDecode->district;
        return $data;
    }
}

 

posted @ 2017-01-11 19:48  狂奔的蜗牛Snails  阅读(485)  评论(0编辑  收藏  举报