This article mainly shares with you how to obtain the user's openid in the WeChat applet. There are two ways to obtain the user's openid in the WeChat applet. I hope it can help everyone.
Method 1:
First obtain the user information (wx.getUserInfo), and then decrypt the sensitive information containing openid in the returned user information to obtain the user's openid. One is to obtain the user information first, and then return The sensitive information containing openid in the user information is decrypted to obtain the user's openid. This method is more troublesome to obtain the user's openid. It also involves data decryption, and if the user refuses authorization, it cannot be obtained. This method will not be described in detail here, please refer to the documentation for details.
Method 2:
First log in (wx.login) to obtain the user login credentials (code), then use this code as a parameter to call the interface, and obtain the user's openid through the backend.
The applet code is as follows:
//app.js App({ onLaunch: function() { wx.login({ success: function(res) { if (res.code) { //发起网络请求 wx.request({ url: 'https://test.com/onLogin', data: { code: res.code } }) } else { console.log('获取用户登录态失败!' + res.errMsg) } } }); } })
The backend data acquisition method is as follows:
//获取用户openid function getopenid(){ $js_code = I('post.code'); if(empty($js_code)) return array('status'=>0,'info'=>'缺少js_code'); $appid = 'xxxxxxxxxxxxx'; $appsecret = 'xxxxxxxxxxxxxxxxxxxxxx'; $curl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code'; $curl = sprintf($curl,$appid,$appsecret,$js_code); $result = request($curl); return array('status'=>1,'info'=>json_decode($result,true)); }
Data return instructions
//正常返回的JSON数据包 { "openid": "OPENID", "session_key": "SESSIONKEY", "unionid": "UNIONID" } //错误时返回JSON数据包(示例为Code无效) { "errcode": 40029, "errmsg": "invalid code" }
Related recommendations:
How to obtain openid in WeChat
How to obtain openid and user information in WeChat applet
The above is the detailed content of How to get the user's openid in the WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.