欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信网页授权

程序员文章站 2024-01-09 22:28:46
...

微信网页授权一共分为以下几步:

(我用的微信测试号,授权之前要配置好授权回调域名,如为百度:www.baidu.com,只此格式,不要加http以及后面不要加任何路径和/等其他字符)

微信网页授权

不了解的可以先看看微信官方文档:点击打开链接

整体代码流程:

1.wechat.php页面方法中调用webAuthorize()授权方法

2.授权方法中又包括webCode()获取code方法

3.授权完成,根据授权方法中回调地址访问其路径,即wechatUser.php

4.wechetUser.php页面方法调用webUser()方法来拉取微信用户信息

5.webUser()方法又包含webAccessToken()方法,获取到token后拉取用户信息并返回


以下是我写的授权流程代码:

1.访问调取网页授权方法:wechat.php

    /**
     * 调用网页授权
     */
    public function actionWebAuthorize()
    {
        $return_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $wechat = new LibWechat();     //微信相关所有方法封装的一个类库
        if (empty($_SESSION['user_openid'])) {
            $wechat->webAuthorize($return_url); //调用网页授权方法
        }
    }

2.调取网页授权所有类方法,libWecaht.php类文件

class LibWechat extends Base
{
    private $appid = '';    //自己appid
    private $appsecret = ''; //自己appsecret
    private $_access_token; //微信网页授权
    public $error;
    public $return_url; //用户同意授权跳转页面
    public $path;  //用户授权前所在页面(用于授权后回调)
    public $code;  //授权code
    public $open_id; //openid,用户唯一凭证

   /**
     * 调取网页授权信息
     */
    public function webAuthorize($return_url)
    {
        $this->path = 'http://www.aa.com/wechatuser/user'; //授权后回调地址
        $this->return_url = $return_url;
        $this->webCode();
    }

    /**
     * 获取微信网页授权code
     */
    public function webCode($user_info = true)
    {
        if (empty($this->path)) {
            $this->error = "跳转地址不能为空";
            return false;
        }
        $scope = $user_info ? 'snsapi_userinfo' : 'snsapi_base';

        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->appid . '&redirect_uri=' . urlencode($this->path) . '&response_type=code&scope=' . $scope . '&state=' . urlencode($this->return_url) . '#wechat_redirect';
        header('location:' . $url);
    }

    /**
     * 获取网页授权 _access_token
     */
    public function webAccessToken()
    {
        $this->code = $_GET['code'];
        if (empty($this->code)) {
            $this->error = "code不能为空";
            return false;
        }
        if (empty($_SESSION['access_token'])) {
            $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->appsecret . '&code=' . $this->code . '&grant_type=authorization_code';
            $result = json_decode($this->requestUrl($url), true);

            if (empty($result['access_token'])) {
                $this->error = $result['errorMsg'];
                return false;
            }
            $_SESSION['access_token'] = $result['access_token'];
            $_SESSION['user_openid'] = $result['openid'];
            $_SESSION['user_unionid'] = $result['unionid'];

            $this->open_id = $result['openid'];
            $this->_access_token = $result['access_token'];
        }
        $this->_access_token = $_SESSION['access_token'];
    }

    /**
     * 获取用户基本信息
     */
    public function webUser()
    {
        if (empty($this->_access_token) || empty($this->open_id)) {
            $this->webAccessToken();
        }
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $this->_access_token . '&openid=' . $this->open_id . '&lang=zh_CN';
        return json_decode($this->requestUrl($url), true);
    }


    /**
     * curl 请求
     */
    public function requestUrl($url, $data = '')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //禁止 cURL 验证对等证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 为SSL不检查名称
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

        if (!empty($data)) {

            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $result = curl_exec($ch);

        curl_close($ch);
        return $result;
    }

3.授权后回调到另一个页面,处理业务逻辑,wecahtUser.php

    /**
     * 网页授权后业务处理
     */
    public function actionUser()
    {
        $wechat = new LibWechat();
        $web_users = $wechat->webUser();
        print_r($web_users);
    }






相关标签: 微信网页授权