第三方微信登陆Demo

3 篇文章 0 订阅
//微信登陆
public function wx_login(){
    $appid = 'wx74718509b6688f2a';
    $url = "https://open.weixin.qq.com/connect/qrconnect";
    $redirect_uri = UrlEncode(site_url('member/wx_back'));
    $state = '3d6be0a4035d839573b04816624a415e'; 
    $_SESSION['state'] = $state; 
    $login_url = "https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_login&state=3d6be0a4035d839573b04816624a415e#wechat_redirect";
    header("Location:$login_url");
}
//微信回调
public function wx_back(){
    $code = $_GET['code'];
    $state = $_GET['state'];
    if($_SESSION['state']==$state){
        $login_url = site_url("member/wx_token/$code");
        header("Location:$login_url");
    }
}
//微信token
public function wx_token($code){
    $appid = 'wx74718509b6688f2a';
    $secret = '7a56bb76387b5f25874adc5037991c1d';
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
    $response = $this->get_contents($url);
    $tokens = json_decode($response);
    $token = $tokens->access_token;
    $openid = $tokens->openid;
    if($token){
        if($this->user->qqlogin_sel($openid)){
            redirect('member/index_member','location');
        }else{
            redirect("member/wx_userinfo/$token/$openid",'location');
        }
    }else{
        redirect('member/wx_login','location');
    }
}
//微信user
public function wx_userinfo($token,$openid){
    $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid";
    $response = $this->get_contents($url);
    $users = json_decode($response);
    $nickname = $users->nickname;
    if($this->user->qq_insert($openid,$nickname)){
        if($this->user->qqlogin_sel($openid)){
            redirect('member/index_member','location');
        }else{
            echo '登陆失败';
        }
    }else{
        echo '登陆失败';
    }
}
//抓取网页、模拟url访问
public function get_contents($url){

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    $response =  curl_exec($ch);
    curl_close($ch);

    return $response;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值