版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/y_z_w123/article/details/86644074
官方文档
说明:以后的章节测试功能基于微信测试平台
网页授权
测试号登录地址
所有操作都是基于laravel框架进行测试
里面的封装的httpGet()请看https://blog.csdn.net/y_z_w123/article/details/81456540
第一步:配置域名
注意点:只有域名不要带http:// 或者 https://
第二步:看文档说明
第三步:模拟网页授权
准备:微信开发者工具
客户端
<a href="线上能请求到的地址?parameters=parameters">网页授权</a> 这个parameters可选 可以传给state
服务端
#用户要点击的链接 要请求到profile这个方法
public function profile()
{
配置回调域不要http只要域名
# $scope = "snsapi_base and snsapi_userinfo"; 这两个的区别已经在上边解释过
$redirect = "域名/请求到code方法";
$redirect = urlencode($redirect);
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxxxxxx&redirect_uri=".$redirect."&response_type=code&scope=snsapi_base&state=yourparameters#wechat_redirect";
return redirect($url); laravel框架自带的跳转
}
public function code(Request $request)
{
$input = $request->all();
$appid = "xxxxxxxxxxxxxxx";
$secret = "xxxxxxxxxxxxxxxxxx";
$code = $input['code']; 接收上面profile传出来的code 参数(必须) 和state参数(可选)
$tokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
$result = json_decode($this->httpGet($tokenUrl),true);
$token = $result['access_token'];
$openid = $result['openid'];
var_dump($openid);
# scope = snsapi_userinfo 的情况下才用的到
$profile = "https://api.weixin.qq.com/sns/userinfo?access_token=".$token."&openid=".$openid."&lang=zh_CN";
$data = json_decode($this->httpGet($profile),true);
return json_encode($data);
}
效果图
scope 为snsapi_base的情况(获取用户的openid属于静默授权)
scope 为snsapi_userinfo的情况(获取用户更多的信息属于低级授权)