关闭

第三方微信登陆Demo

标签: url微信函数
253人阅读 评论(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;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20298次
    • 积分:540
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:21篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论