ThinkPHP网页端网站应用接入微信登录

1.首先要登录微信开放平台:微信开放平台 (qq.com)

 2.创建网站应用,按照要求填写即可

需要注意的一点是,授权回调域必须填写域名,不要填写错误

3.创建完成后就能够获得AppID和AppSecret,需要记录好,如果后面记不得了的话就得重置了。

4.这时候就要进入代码部分了,

js:

        $(".wx").click(function(){
            window.location.href = "http://191app.com/user/weixin";
        });

php:(Thinkphp3.2框架)

  /**
    *微信登陆 
     **/
    public function weixin()
    {
        // $pay_url = SITE_URL;
        //-------配置
        $href = $_SERVER['HTTP_REFERER'];
        cookie('href', $href, 3600000);
        $AppID = 'wx37xxxxxxxxxxxxx';//这里就需要上一步得到的AppID和AppSecret
        $AppSecret = '864c0xxxxxxxxxxxxxxxxxxxxxxxxxxx';
        $callback  = SITE_URL . 'user/weixin_callback'; //回调地址,这里的地址一定要与授权回调域填写的域名是同域名下
        //微信登录
        session_start();
        //-------生成唯一随机串防CSRF攻击
        $state  = md5(uniqid(rand(), TRUE));
        $_SESSION["wx_state"]    = $state; //存到SESSION
        $callback = urlencode($callback);
        $wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=" . $AppID . "&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
        header("location: $wxurl"); 
    }
    /**
    *微信登陆回调
     **/
    public function weixin_callback()
    {
        if ($_GET['code'] != "") {
            $AppID = 'wx37xxxxxxxxxxxxx';
$AppSecret = '864c0xxxxxxxxxxxxxxxxxxxxxxxxxxx';
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $AppID . '&secret=' . $AppSecret . '&code=' . $_GET['code'] . '&grant_type=authorization_code'; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_URL, $url); $json = curl_exec($ch); curl_close($ch); $arr = json_decode($json, 1); if (isset($arr['errcode'])) { echo $arr['errmsg']; exit; } //得到 access_token 与 openid $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $arr['access_token'] . '&openid=' . $arr['openid'] . '&lang=zh_CN'; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_URL, $url); $json = curl_exec($ch); curl_close($ch); $arr = json_decode($json, 1);//这里的$arr就是得到的用户信息,里面有openid和unionid 等用户的数据
$user_model = D('Member'); $openid=$arr['openid']; $unionid = $arr['unionid']; $from = I("get.from",0,"intval"); $channel = I("get.channel",0,"intval"); $username = $this->generate_str(10);//生成10位随机数作为随机账号 $password = '1911234567890';//密码 $wx_uinfo = $user_model->checkWeixinUser($openid);//查看是否存在该用户 if($wx_uinfo){
          //如果存在的话进行登录操作
$user_model->login($wx_uinfo); unset($_SESSION['loginfail']); $href = SITE_URL . 'bxcq/server'; echo "<meta http-equiv=refresh content='0; url=$href'>"; }else{
          //不存在的话注册用户
//插入用户表,存入MEMCACHE SESSION $user_model->register($username, md5($username.$password),$from,0,$channel,1,$openid,$unionid); $href = SITE_URL . 'bxcq/server'; echo "<meta http-equiv=refresh content='0; url=$href'>"; } } }

最终的效果就是这样的了:

 

 

微信开放平台 (qq.com)

原文地址:https://www.cnblogs.com/weixiaofantasy/p/14681311.html