作者:田小多 | 来源:互联网 | 2023-09-17 17:51
这两天在网上看到关于微信网页授权的文档,闲着没事就来做做
首先说说需要的材料
1. 一个可访问的域名或IP地址
2.微信公众平台(一定要先去https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index里面设置网页授权获取用户基本信息的网页账号)
3.草料二维码制作
首先说明一下,我用的是laravel框架
行,不废话了,代码直接上
public function getUserDetail(){$redirect_url=urlencode("https://www.cacov.cn/crontab/getUserInfo");$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->appid&redirect_uri=$redirect_url&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";return redirect($url);}public function getUserInfo(){$code=$_GET['code'];$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appid&secret=$this->appsecret&code=$code&grant_type=authorization_code";$res=$this->my_curl_wx_api($url,'GET');$access_token=$res['access_token'];$openid=$res['openid'];$user_url="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";$user_res=$this->my_curl_wx_api($user_url,'GET');print_r($user_res);}protected function my_curl_wx_api($url, $type = 'POST', $data = array()){$ch = curl_init();curl_setopt($ch, CURLOPT_URL,$url);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);curl_setopt($ch, CURLOPT_HEADER,0);curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);curl_setopt($ch, CURLOPT_AUTOREFERER, 1);if($data){curl_setopt($ch, CURLOPT_POSTFIELDS, $data);}$result = curl_exec($ch);curl_close($ch);return json_decode($result, true);}完成上面的步骤后,就可以去草料二维码里面将getUserDetail方法的服务器位置生成一个二维码,然后用微信扫一下就好了。