Home > Backend Development > PHP Tutorial > PHP implements WeChat web page authorized login

PHP implements WeChat web page authorized login

不言
Release: 2023-03-24 06:04:02
Original
9745 people have browsed it

The main content of this article is about PHP's implementation of WeChat web page authorization login. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it.

The company requires WeChat authorization. After some research, the framework is tp3.2.

Official development document address

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

1. Identify the browser, and the ordinary browser will jump to the login page; if WeChat is opened, initiate WeChat web page authorization Log in, After the WeChat user allows authorization of third-party applications, WeChat will launch the application or redirect to the third-party website, and bring the authorization temporary ticket code parameter;

2. Add AppID and AppSecret through the code parameter, and exchange access_token through API;

3.Make interface calls through access_token to obtain user basic information Data resources may help users implement basic operations.

<?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);
    }
}
Copy after login
<br/>
Copy after login

Related recommendations:

php implements a log function

php implements product addition to shopping Car function (1)_php example

php implements WeChat template message push


The above is the detailed content of PHP implements WeChat web page authorized login. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template