PHP开发网站之微信登录、绑定

PHP开发网站引入第三方登录之微信登录绑定
案例www.spaceyun.com写在前面的话如果在做此项功能之前有去了解OAuth2.0协议那么接下来一切都很容易理解如果没有了解OAuth2.0协议也不影响完成此项功能不过应该是很难领会其原理准备工作微信登录时在微信开放平台open.weixin.qq.com上面注册登录之后*认证开发者资质*不认证是无法开放网页登录授权的),在管理中心添加网站应用相关信息准备齐全通过*审核*之后可以获取到APPID和APPSECERT然后在应用详情里面查看微信登录接口状态如果为已获得表示可以正常开发了需要注意的是网站授权回调域和官网地址是没有http或者https例如我的网站授权回调域是www.spaceyun.com我的官网地址也是www.spaceyun.com),
    数据表用户表里面有openid字段新建一个微信表用来存储微信的openidnickname,headimgurlsex,city,province,country等信息

流程1用户同意授权获取code
    2通过code获取网页授权access_token
    3)刷新access_token(如果需要)
    4拉去用户信息需要scope为snsapi_userinfo正式开发我的开发是在laravel5.2框架里面的所以代码会按照框架的模式不过并没有使用laravel的扩展包纯手工代码应该有参考价值登录和绑定其实是一套流程就算绑定也是操作的snsapi_login的scope接口就是说登录是点击扫码获取openid登录绑定也是点击扫码获取openid绑定先以微信登录为例
    路由Route::get('wxLogin', 'WeixinController@wxLogin');//点击微信登录跳转到的url路由
        Route::get('wxGetCode', 'WeixinController@wxGetCode');//用户扫码之后跳转到的url路由
    控制器先单个方法分析后面补上完整代码1.
        //login
        public function wxLogin(){
            $appid = $this->appid;
            $redirect_uri = urlencode($this->redirect_uri);
            $scope = "snsapi_login";
            $state = "wxLogin";
            $url = "https://open.weixin.qq.com/connect/qrconnect?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
            return redirect($url);
        }
        这个方法用来做微信登录请求触发就是用户点击微信登录提示的链接是跳转到上面的$url,
        其中的appid是在开放平台创建应用后获取的redirect_uri是用户扫码之后的回调地址因为会拼接在下面的url里面需要用urlencode处理scope这里必须是"snsapi_login"state是会提供给回调地址的参数类似于一个场景值的设定可以任意设置用来区分从网站的不同位置发起的请求建议使用有意义的英文单词url是微信开放平台固定的里面的参数替换成我们上面的参数最后做一个跳转我使用的是框架自带的方法可根据实际情况更改这个操作会出现一个展示微信登录二维码的页面这个页面也是微信的页面跟我们本地的服务器没有任何关系2.
        //reback url-->wxGetCode
        public function wxGetCode(){
            $code = $_GET['code'];
            $state = $_GET['state'];

            if($state === 'wxLogin'){
                $this->loginWeixin($code);
            }elseif($state === 'wxBind'){
                $this->bindWeixin($code);
            }else{
                return redirect("http://www.spaceyun.com");
            }
        }
        这是用户扫码之后从微信的二维码页面会跳转到我们服务器的处理页面用户扫码登录成功之后会携带code和state参数提交到我们服务器的服务器的地址我们接收code和state根据state场景值判断做什么处理上面我们是wxLogin会到loginWeixin方法继续处理code3.
        //loginWeixin
        private function loginWeixin($code){
            $appid = $this->appid;
            $appsecert = $this->appsecert;
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecert."&code=".$code."&grant_type=authorization_code";

            //curl模拟get请求,获取结果
            $res = $this->http_curl($url);
            //转化为数组
            $return_data = json_decode($res,true);
            $openid = $return_data['openid'];

            //openid存在,直接登录,openid不存在,先注册再登录
            $result = User::where('wx_openid',$openid)->first();
            if($result){
                //login
                Auth::loginUsingId($result->id,true);

                $jump_url = 'http://www.spaceyun.com/accountSettings';
                echo "<script>window.location.href='".$jump_url."'</script>";
                return redirect('http://www.spaceyun.com/accountSettings');
            }else{
                $access_token = $return_data['access_token'];
                //获取用户基本信息
                $getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
                $userInfo = $this->http_curl($getInfoUrl);
                $weixins = json_decode($userInfo);

                //判断weixins表里面是否存在该条数据
                $weixins_result = Weixin::where('openid',$openid)->first();
                if(empty($weixins_result)){
                    //插入数据
                    Weixin::create([
                        'openid'=>$weixins->openid,
                        'nickname'=>$weixins->nickname,
                        'sex'=>$weixins->sex,
                        'city'=>$weixins->city,
                        'province'=>$weixins->province,
                        'country'=>$weixins->country,
                        'headimgurl'=>$weixins->headimgurl,
                    ]);
                }

                //Register and login
                $user = new User();
                $arr['name'] = $weixins->nickname;
                $arr['email'] = $openid."@fake.com";
                $arr['password'] = "passwd";
                $arr['phone'] = substr($openid,8);
                $arr['wx_openid'] = $openid;
                $insertId = $user->insertGetId($arr);
                Auth::loginUsingId($insertId,true);
                $jump_url = 'http://www.spaceyun.com/accountSettings';
                echo "<script>window.location.href='".$jump_url."'</script>";
                return redirect('http://www.spaceyun.com/accountSettings');
            }

        }
        在这个方法里面根据appidappsecert这两个都是开放平台获取的还有回调返回的code拼接成url然后用curl模拟get请求抓取返回数据我将返回的数据res转化成数组获取到用户的openid和access_token,
        这时在我们的用户表里面一定要有一个叫做微信的openid的字段我的字段名是wx_openid因为我还会有qq_openid和sina_openid,
        我们去用户表查询我们获取到的微信用户openid在不在我们的用户表里面如果存在说明用户是一个老用户直接登录我用的laravel的Auth::loginUsingId($result->id,true)方法完成登录做跳转我跳转到了账号设置页如果不存在说明这是一个新用户新用户需要注册到用户表并且获取相关信息新增到微信表获取用户基本信息需要access_token和openid在上面已经获取到获取到信息判断微信表里面是否存在此条数据不存在就插入到微信表这些数据是微信开放给第三方应用的一些相关信息包括openidnickname,headimgurlsex,city,province,country等信息然后继续在用户表里面插入一条数据因为有一些非空字段我就自定义生成了一些数据最后给这条最新添加的用户做登录登陆之后跳转关于我的跳转多说一句因为用这个框架十来天之前也只开发了一个邮箱相关的功能所以没能明白redirect的原理准备系统研究laravel时再考虑这个我要是只用window.location.href或者只用return redirect都无法实现跳转把两个都写在这个就可以应该是有问题的不过不是错误先这样吧这样登录功能就开发完了绑定功能是相同的流程只是绑定要在登录状态下操作也不用插入用户表只需要改用户表中wx_openid字段需要注意的是一些逻辑上的问题比如说微信号已经绑定过一个账号根据自己应用需求判断允不允许绑定多个等等<?php
namespace App\Http\Controllers;

use App\Weixin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use App\User;
use Cookie;

class WeixinController extends Controller{
    //attribute------开放平台获取
    private $appid = "your APPID";
    private $appsecert = "your APPSECERT";
    //redirect_uri,需要是跳到具体的页面处理,并且要在开放平台创建应用的授权回调域下面
    private $redirect_uri = "http://www.spaceyun.com/wxGetCode";

    //login
    public function wxLogin(){
        $appid = $this->appid;
        $redirect_uri = urlencode($this->redirect_uri);
        $scope = "snsapi_login";
        $state = "wxLogin";
        $url = "https://open.weixin.qq.com/connect/qrconnect?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
        return redirect($url);
    }

    //bind
    public function wxBind(){
        $appid = $this->appid;
        $redirect_uri = urlencode($this->redirect_uri);
        $scope = "snsapi_login";
        $state = "wxBind";
        $url = "https://open.weixin.qq.com/connect/qrconnect?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
        return redirect($url);
    }

    //reback url-->wxGetCode
    public function wxGetCode(){
        $code = $_GET['code'];
        $state = $_GET['state'];

        if($state === 'wxLogin'){
            $this->loginWeixin($code);
        }elseif($state === 'wxBind'){
            $this->bindWeixin($code);
        }else{
            return redirect("http://www.spaceyun.com");
        }
    }

    //loginWeixin
    private function loginWeixin($code){
        $appid = $this->appid;
        $appsecert = $this->appsecert;
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecert."&code=".$code."&grant_type=authorization_code";

        //curl模拟get请求,获取结果
        $res = $this->http_curl($url);

        //转化为数组
        $return_data = json_decode($res,true);
        $openid = $return_data['openid'];

        //openid存在,直接登录,openid不存在,先注册再登录
        $result = User::where('wx_openid',$openid)->first();
        if($result){
            //login
            Auth::loginUsingId($result->id,true);

            $jump_url = 'http://www.spaceyun.com/accountSettings';
            echo "<script>window.location.href='".$jump_url."'</script>";
            return redirect('http://www.spaceyun.com/accountSettings');
        }else{
            //转化为数组
            $access_token = $return_data['access_token'];

            //获取用户基本信息
            $getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
            $userInfo = $this->http_curl($getInfoUrl);
            $weixins = json_decode($userInfo);

            //判断weixins表里面是否存在该条数据
            $weixins_result = Weixin::where('openid',$openid)->first();
            if(empty($weixins_result)){
                //插入数据
                Weixin::create([
                    'openid'=>$weixins->openid,
                    'nickname'=>$weixins->nickname,
                    'sex'=>$weixins->sex,
                    'city'=>$weixins->city,
                    'province'=>$weixins->province,
                    'country'=>$weixins->country,
                    'headimgurl'=>$weixins->headimgurl,
                ]);
            }

            //Register and login
            $user = new User();
            $arr['name'] = $weixins->nickname;
            $arr['email'] = $openid."@fake.com";
            $arr['password'] = "passwd";
            $arr['phone'] = substr($openid,8);
            $arr['wx_openid'] = $openid;
            $insertId = $user->insertGetId($arr);
            Auth::loginUsingId($insertId,true);
            $jump_url = 'http://www.spaceyun.com/accountSettings';
            echo "<script>window.location.href='".$jump_url."'</script>";
            return redirect('http://www.spaceyun.com/accountSettings');
        }

    }


    //bindWeixin
    private function bindWeixin($code){
        $appid = $this->appid;
        $appsecert = $this->appsecert;
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecert."&code=".$code."&grant_type=authorization_code";

        //curl模拟get请求,获取结果
        $res = $this->http_curl($url);

        //转化为数组
        $return_data = json_decode($res, true);
        $access_token = $return_data['access_token'];
        $openid = $return_data['openid'];

        //openid存在,提示需要解绑其他账号,openid不存在,先补充信息到weixins表再绑定
        $result = User::where('wx_openid',$openid)->first();
        if($result){
            $jump_url = 'http://www.spaceyun.com/wxBindError';
            echo "<script>window.location.href='".$jump_url."'</script>";
            return redirect('http://www.spaceyun.com/wxBindError');

        }else{
            $user_id = Auth::id();
            $user = User::find($user_id);
            $user->wx_openid = $openid;
            $user->save();

            //判断weixins表里面是否存在该条数据
            $weixins_result = Weixin::where('openid',$openid)->first();
            if(empty($weixins_result)){
                //获取用户基本信息
                $getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";

                $userInfo = $this->http_curl($getInfoUrl);
                $weixins = json_decode($userInfo);
                Weixin::create([
                    'openid'=>$weixins->openid,
                    'nickname'=>$weixins->nickname,
                    'sex'=>$weixins->sex,
                    'city'=>$weixins->city,
                    'province'=>$weixins->province,
                    'country'=>$weixins->country,
                    'headimgurl'=>$weixins->headimgurl,
                ]);
            }
        }
        $jump_url = 'http://www.spaceyun.com/accountSettings';
        echo "<script>window.location.href='".$jump_url."'</script>";
        return redirect('http://www.spaceyun.com/accountSettings');
    }

    //wxBindError
    public function wxBindError(){
        $wxBindError = '该微信号已被绑定云享客账号,如需更改请到之前账号解除绑定';
        return view("weixin.wxBindError",['information'=>$wxBindError]);
    }

    //解绑
    public function wxUnbind(){
        //users表里面wx_openid字段清空
        $user_id = Auth::id();
        $user = User::find($user_id);
        if(!preg_match("/^1[34578]{1}\d{9}$/",$user->phone)){
            $wxBindError = '请认证手机号之后再解除绑定微信';
            return view("weixin.wxBindError",['information'=>$wxBindError]);
        }else{
            $user->wx_openid = '0';
            $user->save();

            return redirect('/accountSettings');
        }

    }

    //curl模拟get请求
    private function http_curl($url){
        $curlobj = curl_init();
        curl_setopt($curlobj, CURLOPT_URL, $url);
        curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curlobj, CURLOPT_SSL_VERIFYHOST, FALSE);

        $output = curl_exec($curlobj);
        curl_close($curlobj);

        return $output;
    }
}
?>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
PHP网站中实现微信授权登录的步骤如下: 1. 注册微信开放平台账号并创建应用:首先,你需要在微信开放平台注册一个账号,并创建一个应用。在创建应用时,你会获得一个AppID和AppSecret,这些信息将用于后续的授权登录过程。 2. 引入微信登录SDK:在你的PHP网站中,你需要引入微信登录的SDK,以便使用微信提供的API进行授权登录操作。你可以通过Composer安装SDK,或者手动下载SDK并引入到你的项目中。 3. 获取授权链接:在用户点击微信登录按钮时,你需要生成一个授权链接,将用户重定向到该链接。授权链接的生成需要使用你在第一步中获得的AppID和回调URL。回调URL是用户授权后返回的页面,你需要在该页面中处理授权结果。 4. 用户授权登录:当用户点击授权链接后,会跳转到微信登录页面,用户需要输入微信账号密码并确认授权。一旦用户授权成功,微信会将授权结果返回到你在第三步中设置的回调URL。 5. 处理授权结果:在回调URL页面中,你需要解析微信返回的授权结果。授权结果中包含了用户的唯一标识OpenID和访问令牌Access Token。你可以使用这些信息来获取用户的基本信息,例如昵称、头像等。 6. 完成登录流程:根据你的业务需求,你可以将用户的微信信息保存到数据库中,或者直接使用该信息完成用户的登录流程。一旦用户完成登录,你可以在网站中展示用户的个人信息或提供个性化的服务。 下面是一个简单的PHP代码示例,演示了如何实现微信授权登录: ```php <?php require_once 'path/to/wechat-sdk/autoload.php'; use EasyWeChat\Factory; $config = [ 'app_id' => 'your-app-id', 'secret' => 'your-app-secret', 'oauth' => [ 'scopes' => ['snsapi_userinfo'], 'callback' => 'http://your-callback-url', ], ]; $app = Factory::officialAccount($config); $oauth = $app->oauth; // 生成授权链接 $redirectUrl = $oauth->redirect()->getTargetUrl(); // 将用户重定向到授权链接 header("Location: $redirectUrl"); exit; ?> ``` 请注意,上述代码中的`your-app-id`和`your-app-secret`需要替换为你在微信开放平台注册应用时获得的实际值,`http://your-callback-url`需要替换为你的回调URL。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值