search
HomeWeChat AppletMini Program DevelopmentDetailed explanation of WeChat applet authorized login to obtain user information

小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)
     }})
    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;}
    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!

Statement
This article is reproduced at:learnku. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools