明日之星的博客

明日之星技术分享博客

第三方微信登陆Demo

//微信登陆
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;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq3559727/article/details/52369760
文章标签: url 微信 函数
个人分类: php 微信
上一篇网页抓取url替换程序Demo
下一篇去掉CodeIgniter(CI)默认url中的index.php
想对作者说点什么? 我来说一句

第三方微信登录demo

2017年10月29日 2.77MB 下载

微信第三方登录小demo

2016年01月07日 3.06MB 下载

没有更多推荐了,返回首页

关闭
关闭