Detailed explanation of WeChat applet authorized login to obtain user information

coldplay.xixi
Release: 2020-12-24 17:45:05
forward
4004 people have browsed it

小program development tutorialThe column records this blog is only for recording the WeChat applet login process

Detailed explanation of WeChat applet authorized login to obtain user information

##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.

  1. The applet first calls the wx.login() interface Obtain the login credential code, and then call the wx.getUserInfo() interface in the success callback method to obtain the user's basic information (the interface successfully returns encryptedData user sensitive information encrypted data; iv initial vector of the encryption algorithm)

    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)
     }})
    Copy after login
    wx.login Document address: developers.weixin.qq.com/miniprogr...

    wx.getUserInfo document address: developers.weixin.qq.com/miniprogr...

  2. Server : Get the parameter code of the mini program and call the WeChat auth.code2Session interface. If openid, session_key, etc. are returned successfully, then check the official verification and decryption document provided by WeChat

    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;}
    Copy after login
    auth.code2Session interface document: developers.weixin.qq. com/miniprogr...

    Data decryption document (with sample codes in different languages): developers.weixin.qq.com/miniprogr...

Decrypted data:


Detailed explanation of WeChat applet authorized login to obtain user information

Get the decrypted data and save/update the user table.

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template