Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP实现微信网页授权登陆

PHP实现微信网页授权登陆

不言
不言asal
2018-04-16 14:16:099731semak imbas

这篇文章主要介绍的内容是关于PHP实现微信网页授权登陆,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

公司要求有微信授权,自己研究了下,框架是tp3.2.

官方开发文档地址 

https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN;

1.识别浏览器,普通浏览器跳到登陆页面;微信打开的话,发起微信网页授权登陆,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;

2.通过code参数加上AppID和AppSecret等,通过API换取access_token;

3.通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。

<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller {
    /*
    * 自动执行
    */
    public function _initialize(){
        //判断是否在微信打开
        $ua = $_SERVER[&#39;HTTP_USER_AGENT&#39;];
        //MicroMessenger 是android/iphone版微信所带的
        //Windows Phone 是winphone版微信带的  (这个标识会误伤winphone普通浏览器的访问)
        if(strpos($ua, &#39;MicroMessenger&#39;) == false && strpos($ua, &#39;Windows Phone&#39;) == false){
            //普通浏览器
            if(!$_SESSION[&#39;username&#39;]) {
                header(&#39;Location:xxx&#39;);
            }
        }else{  
            //微信浏览器
            $users = M(&#39;User&#39;);
            $appid = &#39;xxx&#39;;
            $secret = &#39;xxx&#39;;
            if(!$_SESSION[&#39;username&#39;]) {
                //微信网页授权
                $redirect_uri = urlencode (&#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;REQUEST_URI&#39;]);
                $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect";
                header("Location:".$url);
                $code = $_GET["code"];

                //第一步:取得openid
                $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
                $oauth2 = $this->getJson($oauth2Url);
                //第二步:根据全局access_token和openid查询用户信息
                $access_token = $oauth2["access_token"];
                $openid = $oauth2[&#39;openid&#39;];
                $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
                $userinfo = $this->getJson($get_user_info_url);
                //save用户信息
                if($userinfo[&#39;openid&#39;]){
                    $username = $userinfo[&#39;openid&#39;];
                    $nickname = $userinfo[&#39;nickname&#39;];
                    $headimg = $userinfo[&#39;headimgurl&#39;];
                    $province = $userinfo[&#39;province&#39;];
                    $city = $userinfo[&#39;city&#39;];
                    $sex = $userinfo[&#39;sex&#39;];
                    $user = $users->where(array(&#39;username&#39; => $username))->find();
                    if ($user) {
                        $users->where(array(&#39;username&#39; => $username))->save(array(&#39;nickname&#39; => $nickname, &#39;avatar&#39; => $headimg, &#39;lasttime&#39; => time()));
                    }else{
                        $users->add(array(&#39;username&#39; => $username, &#39;nickname&#39; => $nickname, &#39;avatar&#39; => $headimg, &#39;province&#39; => $province, &#39;city&#39; => $city, &#39;gender&#39; => $sex, &#39;regtime&#39; => time(), &#39;lasttime&#39; => time()));
                        // $data = array(&#39;username&#39; => $username, &#39;nickname&#39; => $nickname, &#39;avatar&#39; => $headimg, &#39;province&#39; => $province, &#39;city&#39; => $city, &#39;gender&#39; => $sex, &#39;regtime&#39; => time(), &#39;lasttime&#39; => time());
                    }
                    $_SESSION[&#39;username&#39;] = $username;
                    if($user[&#39;tel&#39;] == NULL){
                        //如果用户手机号为空的话跳转到绑定手机号页面
                        header(&#39;Location:xxx&#39;); 
                    }
                }                
            }else{
                $user = D(&#39;User&#39;)->getUserInfo();  //getUserInfo()是model根据session(&#39;username&#39;)获取用户数据的方法
                if($user[&#39;tel&#39;] == NULL){
                    header(&#39;Location:xxx&#39;);
                }
            }

            //获取接口调用凭证access_token
            $accessurl = &#39;https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&#39;.$appid.&#39;&secret=&#39;.$secret;
            $access_token = S(&#39;access_token&#39;);
            if(!$access_token){
                $access = $this->getJson($accessurl);
                if(!empty($access[&#39;access_token&#39;])){
                    S(&#39;access_token&#39;,$access[&#39;access_token&#39;],$access[&#39;expires_in&#39;]);
                }
            }
            //分享
            /*$share = new WechatShare($appid, $_SESSION[&#39;username&#39;]);
            $this->shareScript = $share->getSgin($access_token);
            $this->assign(&#39;shareScript&#39;, $this->shareScript);
            $this->assign(&#39;sharewechaid&#39;, $_SESSION[&#39;username&#39;]);
            if($_GET[&#39;sharewechaid&#39;]){
                $this->assign(&#39;getsharewechaid&#39;, $_GET[&#39;sharewechaid&#39;]);
            }*/ 
        }
    
}

    public function getJson($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }
}
<br/>

  相关推荐:

php实现一个日志功能

php实现产品加入购物车功能(1)_php实例

php实现微信模板消息推送


Atas ialah kandungan terperinci PHP实现微信网页授权登陆. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn