小program development tutorialThe column records this blog is only for recording the WeChat applet login process
##Recommended (free):Mini Program Development Tutorial
WeChat Mini Program Open Document: developers.weixin.qq.com/miniprogr...
This blog is only used to record the WeChat applet login process. For specific codes, please refer to the WeChat documentation.
wx.login({ success: function (e) { var code = e.code;//登录凭证 if (code) { //2、调用获取用户信息接口 wx.getUserInfo({ success: function (res) { console.log({encryptedData: res.encryptedData, iv: res.iv, code: code}); // 将code, encryptedData ,iv 发送给服务端 ,根据服务端返回的数据判断登录成功或者失败 }, fail: function () { console.log('获取用户信息失败') } }) } else { console.log('获取用户登录态失败!' + e.errMsg) } }, fail: function () { callback(false) }})
wx.getUserInfo document address: developers.weixin.qq.com/miniprogr...
public function miniProgramLogin($code){ $url = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=$code&grant_type=authorization_code"; return json_decode(curl_request($url), true);}//助手函数 curl_requestfunction curl_request($url, $data=null, $method='get', $header = array("content-type: application/json"), $https=true, $timeout = 5){ $method = strtoupper($method); $ch = curl_init();//初始化 curl_setopt($ch, CURLOPT_URL, $url);//访问的URL curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//只获取页面内容,但不输出 if($https){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https请求 不验证证书 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//https请求 不验证HOST } if ($method != "GET") { if($method == 'POST'){ curl_setopt($ch, CURLOPT_POST, true);//请求方式为post请求 } if ($method == 'PUT' || strtoupper($method) == 'DELETE') { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式 } curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//请求数据 } curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头 //curl_setopt($ch, CURLOPT_HEADER, false);//设置不需要头信息 $result = curl_exec($ch);//执行请求 curl_close($ch);//关闭curl,释放资源 return $result;}
Data decryption document (with sample codes in different languages): developers.weixin.qq.com/miniprogr...
The above is the detailed content of Detailed explanation of WeChat applet authorized login to obtain user information. For more information, please follow other related articles on the PHP Chinese website!