...
PHP

php微信公众号网页授权登录类

test.php

<?php
require './WxOAuth2.php';
// appID
// wxda89c219fabae304
// appsecret
// 2c8af0dbda9ef9376eea2b78ca98e81d
$wxAuth = new WxOAuth2('wxda89c219fabae304','2c8af0dbda9ef9376eea2b78ca98e81d');
print_r($wxAuth->init());

WxOauth2.php

<?php
/**
 *  微信授权类
 */
class WxOAuth2
{
    private $appid = "";
    private $appsecret = "";
    private $redirect_uri = "";
    private $scope = "";
    private $state = "";

    // 1、上面已经提到,对于以snsapi_base为scope的网页授权,就静默授权的,用户无感知;
    // 2、对于已关注公众号的用户,如果用户从公众号的会话或者自定义菜单进入本公众号的网页授权页,即使是scope为snsapi_userinfo,也是静默授权,用户无感知。
    /**
     * $appid 公众号appid,
     * $appsecret 公众号appsecret,
     * $scope='snsapi_userinfo'|'snsapi_base',
     * $state="" 其他参数
     */
    public function __construct($appid,$appsecret,$scope='snsapi_userinfo',$state="") {
        session_start();
        $this->appid = $appid;
        $this->appsecret = $appsecret;
        $this->redirect_uri = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
        $this->scope = $scope;
        $this->state = $state;
    }

    /**
     * 初始化  用于放置访问页面顶端
     */
    public function init()
    {
        // 获得access_token内容
        $oauth2 = empty($_SESSION['oauth2']) ? false : $_SESSION['oauth2'];
        // 检查access_token是否过期
        if(!$oauth2 || $oauth2['expires_in'] < time()){
            // 如果$access不存在 或者 过期时间小于当前时间则判定token已过期

            // 获取code参数 
            $code = empty($_GET['code']) ? false : $_GET['code'];
            // 检查code是否存在 存在则表示第二次跳转
            if($code){
                // 获取ACCESS_TOKEN
                $this->getAccessToken($code);
                // 获取基本信息则返回openid
                if($this->scope == 'snsapi_base'){
                    return $_SESSION['oauth2'];
                }
            }else{
                // 执行跳转操作
                $this->toAuth();
            }

        }

        // 获取用户信息
        return $this->getUserInfo();
    }
    /**
     * 进入授权链接
     */
    private function toAuth()
    {
        $authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$this->redirect_uri}&response_type=code&scope={$this->scope}&state={$this->state}#wechat_redirect";
        header("Location:{$authUrl}");
        exit;
    }

    /**
     * 获取AccessToken
     */
    private function getAccessToken($code)
    {
        $access_token = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code");
        // "access_token":"ACCESS_TOKEN",
        // "expires_in":7200,
        // "refresh_token":"REFRESH_TOKEN",
        // "openid":"OPENID",
        // "scope":"SCOPE" 
        $access_token = json_decode($access_token,true);
        if(!empty($access_token['errcode'])){
            unset($_SESSION['oauth2']);
            // 获取token失败
            $this->showError($access_token['errcode']);
        }

        $access_token['expires_in'] += time(); 
        // 保存$access_token信息
        $_SESSION['oauth2'] = $access_token;
    }

    /**
     * 获取用户信息
     */
    private function getUserInfo()
    {
        if(empty($_SESSION['userInfo'])){
            $oauth2 = $_SESSION['oauth2'];
            $userInfo = file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token={$oauth2['access_token']}&openid={$oauth2['openid']}&lang=zh_CN");
            $userInfo = json_decode($userInfo,true);
            // "openid":" OPENID",
            // "nickname": NICKNAME,
            // "sex":"1",
            // "province":"PROVINCE",
            // "city":"CITY",
            // "country":"COUNTRY",
            // "headimgurl":       "http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
            // "privilege":[ "PRIVILEGE1" "PRIVILEGE2"     ],
            // "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
            if(!empty($userInfo['errcode'])){
                unset($_SESSION['oauth2']);
                // 获取用户信息失败
                $this->showError($userInfo['errcode']);
            }
            // 写入用户信息到_SESSION
            $_SESSION['userInfo'] = $userInfo;
            return $userInfo;
        }
        return $_SESSION['userInfo'];
    }

    /**
     * 输出提示页面
     */
    private function showError($errcode,$errmsg='')
    {
        $errmsgs = [
            '10003'=>    'redirect_uri域名与后台配置不一致',
            '10004'=>    '此公众号被封禁',
            '10005'=>    '此公众号并没有这些scope的权限',
            '10006'=>    '必须关注此测试号',
            '10009'=>    '操作太频繁了,请稍后重试',
            '10010'=>    'scope不能为空',
            '10011'=>    'redirect_uri不能为空',
            '10012'=>    'appid不能为空',
            '10013'=>    'state不能为空',
            '10015'=>    '公众号未授权第三方平台,请检查授权状态',
            '10016'=>    '不支持微信开放平台的Appid,请使用公众号Appid',
            '40029'=>   'invalid code(无效的code)',
            '40003'=>   'openId无效'
        ];
        if(array_key_exists($errcode,$errmsgs)){
            $errmsg = $errmsgs[$errcode];
        }
        $tpl = <<<eof
        <!DOCTYPE html>
        <html>
            <head>
                <meta charset="utf-8" />
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <title>授权发生错误</title>
            </head>
            <body style="padding: 0;margin: 0;font-size: 15px;">
               <div  style="position: fixed;width: 100%; display: flex;align-items: center; justify-content: center; flex-direction: column;height: 100%;">
                   <div style="text-align: center;">
                       <svg t="1589589394127" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4654" width="120" height="120"><path d="M81.066667 992.426667c0-162.986667 174.933333-294.4 391.68-294.4s391.68 132.266667 391.68 294.4H81.066667z" fill="#B3D3EF" p-id="4655"></path><path d="M379.733333 619.52h186.026667v168.106667H379.733333z" fill="#ECF1F2" p-id="4656"></path><path d="M471.893333 702.293333c33.28 0 64.853333-11.946667 93.013334-31.573333v-51.2c-27.306667 20.48-58.88 31.573333-93.013334 31.573333s-64.853333-11.946667-93.013333-31.573333v51.2c28.16 20.48 59.733333 31.573333 93.013333 31.573333z" fill="#C7CAC7" p-id="4657"></path><path d="M523.1104 823.534933l-50.688 50.688-50.679467-50.688 50.688-50.688z" fill="#48A0DB" p-id="4658"></path><path d="M514.56 992.426667h-82.773333l24.746666-133.12h34.133334z" fill="#48A0DB" p-id="4659"></path><path d="M351.573333 665.6l-70.826666 70.826667 113.493333 113.493333 80.213333-75.946667z" fill="#B3D3EF" p-id="4660"></path><path d="M593.066667 665.6l70.826666 70.826667-113.493333 113.493333-80.213333-75.946667z" fill="#B3D3EF" p-id="4661"></path><path d="M222.914922 390.28246a68.266667 39.253333 71.761 1 0 74.562431-24.571132 68.266667 39.253333 71.761 1 0-74.562431 24.571132Z" fill="#ECF1F2" p-id="4662"></path><path d="M662.838828 443.108761a39.253333 68.266667 18.239 1 0 42.732404-129.673793 39.253333 68.266667 18.239 1 0-42.732404 129.673793Z" fill="#ECF1F2" p-id="4663"></path><path d="M273.066667 372.906667a279.04 199.68 90 1 0 399.36 0 279.04 199.68 90 1 0-399.36 0Z" fill="#FFFFFF" p-id="4664"></path><path d="M674.133333 370.346667c-4.266667-59.733333-14.506667-86.186667-49.493333-135.68-40.106667-57.173333-72.533333-21.333333-120.32 14.506666-60.586667 46.08-125.44-5.973333-163.84-9.386666-36.693333-3.413333-29.866667 21.333333-66.56 130.56 0 14.506667 0.853333 29.866667 2.56 43.52 33.28-102.4 28.16-126.293333 64-122.88 38.4 3.413333 103.253333 55.466667 163.84 9.386666 47.786667-35.84 80.213333-71.68 120.32-14.506666 31.573333 44.373333 42.666667 70.826667 47.786667 119.466666 0.853333-11.946667 1.706667-23.893333 1.706666-34.986666z" fill="#C7CAC7" p-id="4665"></path><path d="M576.853333 70.826667C470.186667-32.426667 148.48 69.12 272.213333 372.906667c36.693333-109.226667 29.866667-133.973333 66.56-130.56 38.4 3.413333 103.253333 55.466667 163.84 9.386666 47.786667-35.84 80.213333-71.68 120.32-14.506666 34.986667 49.493333 44.373333 75.946667 49.493334 135.68 56.32-52.053333 65.706667-279.04-95.573334-302.08z" fill="#424B61" p-id="4666"></path><path d="M753.493333 1006.933333s181.76-120.32 204.8-181.76c41.813333-107.52 33.28-262.826667 33.28-262.826666 0-31.573333-213.333333-118.613333-238.08-118.613334-25.6 0-238.08 87.04-238.08 119.466667 0 0-8.533333 154.453333 33.28 262.826667 23.04 60.586667 204.8 180.906667 204.8 180.906666z" fill="#FFFFFF" p-id="4667"></path><path d="M753.493333 945.493333c-78.506667-55.466667-148.48-116.906667-157.013333-138.24-29.866667-77.653333-30.72-186.88-29.866667-226.133333 34.986667-24.746667 151.04-75.093333 187.733334-84.48 36.693333 9.386667 152.746667 59.733333 187.733333 84.48 0.853333 39.253333 0 148.48-29.866667 226.133333-10.24 21.333333-80.213333 82.773333-158.72 138.24z" fill="#EB6248" p-id="4668"></path><path d="M753.493333 555.52c35.84 9.386667 151.04 58.88 186.88 83.626667 0.853333-24.746667 0.853333-46.08 0.853334-58.88-34.986667-24.746667-151.04-75.093333-187.733334-84.48-36.693333 9.386667-152.746667 59.733333-187.733333 84.48 0 12.8 0 34.133333 0.853333 58.88 35.84-24.746667 150.186667-74.24 186.88-83.626667z" fill="#BC4F3A" p-id="4669"></path><path d="M850.773333 771.413333l-61.44-61.44 61.44-61.44-35.84-36.693333-61.44 61.44-61.44-61.44-36.693333 36.693333 61.44 61.44-61.44 61.44 36.693333 35.84 61.44-61.44 61.44 61.44z" fill="#FFFFFF" p-id="4670"></path><path d="M1000.106667 563.2c0-7.68 0-26.453333-114.346667-79.36C833.706667 460.8 768.853333 435.2 753.493333 435.2c-11.093333 0-45.226667 11.946667-81.92 26.453333l2.56-10.24c22.186667-0.853333 46.08-24.746667 58.026667-58.88 12.8-38.4 5.12-75.093333-17.92-86.186666 8.533333-40.106667 7.68-87.893333-5.12-128.853334-19.626667-64.853333-64.853333-105.813333-127.146667-115.2-58.026667-53.76-170.666667-52.906667-251.733333 2.56-39.253333 26.453333-116.906667 98.133333-87.04 239.786667-1.706667 0-4.266667 0.853333-5.973333 1.706667-25.6 8.533333-34.986667 46.933333-21.333334 87.893333 11.093333 34.986667 34.986667 58.026667 58.026667 58.88 15.36 73.386667 51.2 133.973333 98.986667 170.666667v47.786666L358.4 658.773333c-3.413333-3.413333-8.533333-2.56-11.946667 0l-68.266666 68.266667c-122.026667 52.053333-204.8 151.04-204.8 264.533333 0 5.12 3.413333 8.533333 8.533333 8.533334h647.68c-1.706667-0.853333-3.413333-2.56-5.12-4.266667 14.506667 10.24 23.893333 16.213333 25.6 17.066667 1.706667 0.853333 3.413333 1.706667 5.12 1.706666s3.413333-0.853333 5.12-1.706666c1.706667-0.853333 11.093333-7.68 25.6-17.066667-1.706667 1.706667-3.413333 2.56-5.12 4.266667h87.04c5.12 0 8.533333-3.413333 8.533333-8.533334 0-19.626667-2.56-39.253333-7.68-57.173333 44.373333-35.84 88.746667-77.653333 99.84-106.666667 39.253333-107.52 31.573333-258.56 31.573334-264.533333zM709.12 325.12c10.24 9.386667 15.36 34.133333 5.973333 63.146667-8.533333 26.453333-24.746667 42.666667-38.4 46.933333 3.413333-18.773333 5.12-38.4 5.12-58.88 11.946667-11.946667 21.333333-30.72 27.306667-51.2z m-477.013333 63.146667c-11.093333-33.28-3.413333-61.44 11.093333-66.56 1.706667-0.853333 2.56-0.853333 4.266667-0.853334 4.266667 17.066667 10.24 34.133333 17.92 52.906667 0 20.48 1.706667 40.96 5.12 60.586667-13.653333-3.413333-29.866667-20.48-38.4-46.08z m42.666666-49.493334l-2.56 7.68-5.12-15.36c0-0.853333 0-0.853333-0.853333-1.706666-2.56-7.68-4.266667-15.36-5.973333-23.04-29.866667-134.826667 42.666667-203.093333 79.36-227.84C414.72 28.16 520.533333 27.306667 571.733333 76.8c1.706667 0.853333 2.56 1.706667 5.12 2.56 72.533333 10.24 103.253333 62.293333 116.053334 104.106667 12.8 40.96 12.8 85.333333 5.12 121.173333-1.706667 6.826667-3.413333 12.8-5.12 18.773333-3.413333 9.386667-6.826667 18.773333-11.093334 25.6 0 0.853333-0.853333 1.706667-0.853333 2.56l-2.56-17.92c-0.853333-6.826667-2.56-12.8-4.266667-18.773333-5.973333-21.333333-14.506667-40.106667-28.16-62.293333-4.266667-5.973333-8.533333-12.8-13.653333-19.626667-13.653333-19.626667-28.16-30.72-43.52-33.28-2.56 0-4.266667-0.853333-6.826667-0.853333-22.186667 0-43.52 16.213333-67.413333 34.986666-4.266667 3.413333-9.386667 7.68-14.506667 11.093334-40.106667 29.866667-81.066667 14.506667-116.906666 0.853333-14.506667-5.12-28.16-11.093333-40.106667-11.946667-23.04-1.706667-32.426667 5.973333-40.96 25.6-4.266667 10.24-7.68 23.04-12.8 40.106667-1.706667 5.973333-3.413333 12.8-5.973333 20.48-4.266667 5.973333-5.973333 11.946667-8.533334 18.773333zM290.133333 448.853333c-1.706667-5.973333-2.56-11.946667-3.413333-18.773333-2.56-17.92-4.266667-37.546667-4.266667-56.32l5.12-15.36c2.56-8.533333 5.12-16.213333 6.826667-23.04 3.413333-11.946667 6.826667-22.186667 9.386667-31.573333 14.506667-52.053333 17.066667-55.466667 35.84-53.76 9.386667 0.853333 22.186667 5.973333 35.84 11.093333 36.693333 13.653333 86.186667 32.426667 133.973333-3.413333 5.12-3.413333 10.24-7.68 14.506667-11.093334 23.893333-17.92 43.52-34.133333 61.44-31.573333 11.093333 1.706667 21.333333 10.24 32.426666 25.6 23.893333 34.133333 34.986667 56.32 41.813334 86.186667 1.706667 7.68 2.56 16.213333 4.266666 25.6 0.853333 5.973333 1.706667 12.8 1.706667 19.626666 0 19.626667-1.706667 39.253333-4.266667 57.173334-0.853333 5.973333-1.706667 12.8-3.413333 18.773333-1.706667 7.68-3.413333 14.506667-5.12 21.333333-10.24 4.266667-20.48 8.533333-30.72 13.653334-114.346667 52.053333-114.346667 71.68-114.346667 78.506666 0 2.56-1.706667 33.28 0 75.946667-11.093333 2.56-22.186667 4.266667-33.28 4.266667-22.186667 0-42.666667-5.12-62.293333-14.506667-9.386667-4.266667-18.773333-10.24-27.306667-17.066667-46.08-33.28-80.213333-91.306667-94.72-161.28z m224.426667 275.626667l-40.96 36.693333-58.026667-51.2-17.066666-15.36-9.386667-8.533333v-51.2c25.6 16.213333 54.613333 24.746667 84.48 24.746667 11.946667 0 23.04-1.706667 34.133333-4.266667 0.853333 21.333333 3.413333 45.226667 6.826667 69.12z m-40.96 63.146667l8.533333 8.533333 29.866667 28.16-25.6 25.6h-25.6l-25.6-25.6 29.866667-28.16 8.533333-8.533333z m11.946667-14.506667l32.426666-29.013333c5.12 28.16 11.946667 57.173333 22.186667 83.626666l-8.533333-8.533333-0.853334-0.853333-45.226666-45.226667z m-168.96-59.733333l35.84-35.84 18.773333 17.066666 5.12 4.266667 11.946667 11.093333 4.266666 3.413334 68.266667 60.586666-45.226667 45.226667-0.853333 0.853333-20.48 19.626667-98.133333-100.693333-2.56-2.56 23.04-23.04z m109.226666 270.506666H90.453333c4.266667-101.546667 78.506667-189.44 188.586667-238.08l110.933333 110.933334c1.706667 1.706667 4.266667 2.56 5.973334 2.56 1.706667 0 4.266667-0.853333 5.973333-2.56l21.333333-20.48 25.6 25.6-23.04 122.026666z m17.92 0l19.626667-107.52 1.706667-9.386666h19.626666l1.706667 6.826666 19.626667 109.226667H443.733333z m79.36 0l-22.186666-123.733333 23.893333-23.893333 21.333333 20.48c1.706667 1.706667 3.413333 2.56 5.973334 2.56 2.56 0 4.266667-0.853333 5.973333-2.56 33.28 42.666667 101.546667 94.72 147.626667 128H523.093333z m19.626667-149.333333s0 0.853333 0.853333 0.853333l-0.853333-0.853333z m4.266667 6.826667s0 0.853333 0 0c0 0.853333 0 0 0 0z m5.12 7.68z m303.786666 134.826666H802.133333c14.506667-10.24 31.573333-23.04 49.493334-37.546666 2.56 12.8 4.266667 24.746667 4.266666 37.546666z m-67.413333 9.386667z m6.826667-4.266667z m155.306666-167.253333c-20.48 52.906667-168.96 155.306667-197.12 174.933333-28.16-19.626667-176.64-122.026667-197.12-174.933333-40.106667-104.96-32.426667-257.706667-32.426666-259.413333 0.853333-5.973333 24.746667-27.306667 106.666666-64.853334 61.44-28.16 114.346667-46.08 122.88-46.08s61.44 17.92 122.88 46.08c82.773333 37.546667 105.813333 58.88 106.666667 64.853334 0 2.56 7.68 155.306667-32.426667 259.413333z" p-id="4671"></path><path d="M945.493333 573.44c-36.693333-26.453333-154.453333-76.8-190.293333-86.186667H750.933333c-36.693333 9.386667-154.453333 59.733333-190.293333 86.186667-2.56 1.706667-3.413333 4.266667-3.413333 6.826667-0.853333 42.666667 0.853333 151.04 30.72 229.546666 10.24 26.453333 88.746667 92.16 160.426666 142.506667 1.706667 0.853333 3.413333 1.706667 5.12 1.706667s3.413333-0.853333 5.12-1.706667c71.68-50.346667 150.186667-116.053333 160.426667-142.506667 29.866667-78.506667 31.573333-186.88 30.72-229.546666-0.853333-2.56-1.706667-5.12-4.266667-6.826667z m-42.666666 230.4c-6.826667 17.066667-64.853333 71.68-149.333334 131.413333-84.48-59.733333-142.506667-114.346667-149.333333-131.413333-28.16-73.386667-30.72-174.08-29.866667-218.453333 37.546667-24.746667 143.36-70.826667 179.2-80.213334 35.84 9.386667 141.653333 55.466667 179.2 80.213334 0 44.373333-1.706667 145.066667-29.866666 218.453333z" p-id="4672"></path><path d="M856.746667 642.56L820.906667 605.866667c-1.706667-1.706667-3.413333-2.56-5.973334-2.56s-4.266667 0.853333-5.973333 2.56l-55.466667 55.466666-55.466666-55.466666c-1.706667-1.706667-3.413333-2.56-5.973334-2.56s-4.266667 0.853333-5.973333 2.56l-35.84 35.84c-1.706667 1.706667-2.56 3.413333-2.56 5.973333s0.853333 4.266667 2.56 5.973333l55.466667 55.466667-55.466667 55.466667c-1.706667 1.706667-2.56 3.413333-2.56 5.973333s0.853333 4.266667 2.56 5.973333l35.84 35.84c3.413333 3.413333 8.533333 3.413333 11.946667 0l55.466666-55.466666 55.466667 55.466666c1.706667 1.706667 4.266667 2.56 5.973333 2.56s4.266667-0.853333 5.973334-2.56l35.84-35.84c3.413333-3.413333 3.413333-8.533333 0-11.946666l-55.466667-55.466667 55.466667-55.466667c3.413333-2.56 3.413333-7.68 0-11.093333z m-73.386667 61.44c-3.413333 3.413333-3.413333 8.533333 0 11.946667l55.466667 55.466666-23.893334 23.893334-55.466666-55.466667c-3.413333-3.413333-8.533333-3.413333-11.946667 0l-55.466667 55.466667-23.893333-23.893334 55.466667-55.466666c1.706667-1.706667 2.56-3.413333 2.56-5.973334s-0.853333-4.266667-2.56-5.973333L668.16 648.533333l23.893333-23.893333 55.466667 55.466667c1.706667 1.706667 3.413333 2.56 5.973333 2.56s4.266667-0.853333 5.973334-2.56l55.466666-55.466667 23.893334 23.893333-55.466667 55.466667z" p-id="4673"></path></svg>
                   </div>
                   <h3>
                       授权发生错误
                   </h3>
                   <div style="color: #e51a00;padding: 0 40px;line-height: 1.8;">
                       <div style="color: #666;font-weight: bold;">
                           错误代码:{$errcode}
                       </div>{$errmsg}
                   </div>
                   <div style="color: #aaa;padding: 30px; font-size: .9rem;">
                       请关闭网页重新打开网址,再试一次~
                   </div>
               </div>
            </body>
        </html>
        eof;
        echo $tpl;
        exit;
    }
}
一行代码手机端使用开发者工具调试网页 php身份证号验证
biu biu biu
js 数字转中文 阿拉伯数字转中文大写 LayerUI 子框架关闭父框架 js实现移动端双指缩放和旋转 php 图像压缩 png透明图像压缩 php curl ip伪造