admin管理员组文章数量:1532656
转载自:https://blog.csdn/phpzhi/article/details/81837565
无废话讲解:
网站:
https://open.weixin.qq/
1.网页授权登陆地址:https://developers.weixin.qq/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
微信公众号后台-添加回调地址的域名
https://mp.weixin.qq/cgi-bin/settingpage?t=setting/function&action=function&token=829013267&lang=zh_CN
【设置->公众号设置->功能设置->网页授权域名】-否则error:10003
2.发送get请求
$appid = 'wxcxxxxxxxxx1';
$redirect_uri = urlencode('https://gxxxxxxxoge/api/wecat/wx__notify');//重定向地址
$scope = 'snsapi_userinfo';
$url = "https://open.weixin.qq/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/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/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = $this->getJson($get_user_info_url);
//打印用户信息
print_r($userinfo);
}
版权声明:本文标题:微信公众号-网页授权登陆 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1725775128a1041833.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论