微信公众号-网页授权登陆

10 篇文章 0 订阅

转载自:https://blog.csdn.net/phpzhi/article/details/81837565

无废话讲解:

网站:
https://open.weixin.qq.com/

1.网页授权登陆地址:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html

微信公众号后台-添加回调地址的域名
https://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/function&action=function&token=829013267&lang=zh_CN

【设置->公众号设置->功能设置->网页授权域名】-否则error:10003
在这里插入图片描述

2.发送get请求

		$appid = 'wxcxxxxxxxxx1';
        $redirect_uri = urlencode('https://gxxxxxxxoge.com/api/wecat/wx__notify');//重定向地址
        $scope = 'snsapi_userinfo';

        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=$scope&state=1#wechat_redirect";
        header("Location:" . $url);

        $result = file_get_contents($url);

请求后回调的内容 eg:
在这里插入图片描述

2.获取code后,请求以下链接获取access_token:

/**
     * 微信公众号登陆
     *
     * @param Request $request
     * @author deng    (2019/11/14 11:36)
     */
    public function wx_login_notify(Request $request)
    {
        $code = $_GET["code"];
        if (empty($code)) {
            Log::record($request->param(),'demo');
            exit;
        }
        $config_open = \think\facade\Config::get()['open']['wechat_official_account'];
        $appid = $config_open['appid'];
        $secret = $config_open['secret'];
        $code = $_GET["code"];

        $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
        $oauth2 = $this->getJson($oauth2Url);

        if (empty($oauth2['openid'])) {
            //  openid没有获取到时,记录log
            Log::record($oauth2,'demo');
            exit;
        }

        // 获得 access_token 和openid
        $access_token = $oauth2["access_token"];
        $openid = $oauth2['openid'];
        //  通过code,获取
        $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);

        //打印用户信息
        print_r($userinfo);


    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值