首頁 >後端開發 >php教程 >PHP實現微信網頁授權登陸

PHP實現微信網頁授權登陸

不言
不言原創
2018-04-16 14:16:099768瀏覽

這篇文章主要介紹的內容是關於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實作微信模板訊息推送


以上是PHP實現微信網頁授權登陸的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn