


How to obtain user UnionID, nickname, and avatar information in mini program development
The content of this article is about the method of obtaining user UnionID, nickname, and avatar information in the development of small programs. It has certain reference value and friends in need can refer to it. I hope it helps you.
I have been developing a small program recently and recorded some of it.
In the past, you could use wx.getUserInfo to obtain user information, but then the official made adjustments, so you have to change your mind.
Obtaining user nicknames and avatars
This step is very convenient. It can be achieved using the built-in components of the mini program. You can obtain the following data
As you can see, there is still a lot of relevant information that can be obtained. The following is an example of avatar and nickname
<!-- 头像 --> <open-data></open-data> <!-- 昵称 --> <open-data></open-data>
Get the user’s UnionID
Get the user’s nickname , The avatar is very simple, but in actual development, we often need the user's UnionID, which can be achieved using wx.login and wx.request. Let’s take a look at the official process first
You can see that the process is not complicated. The following is the JS example of the mini program
onLoad: function (options) { var that = this; wx.login({ success: function (res) { if (res.code) { // 发起网络请求,获取用户UnionID wx.request({ url: 'https://xxxx', data: { code: res.code }, success: function (res) { if (res.data.message == 'success') { // 获取数据成功 console.log(res.data.data) } } }) } else { // 获取code失败 console.log('登录失败!' + res.errMsg) } } }); }
The following is the back-end PHP code
<?php $code = $_GET['code']; $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $this->appid . '&secret=' . $this->secret . '&js_code=' . $code . '&grant_type=authorization_code'; $userInfo = file_get_contents($url); $userInfo = json_decode($userInfo, true); if (!$userInfo['unionid']) { echo json_encode(array('data'=>'','message'=>'error')); } else { echo json_encode(array('data'=> $userInfo['unionid'],'message'=>'success')); }
After obtaining the UnionID, you can continue the business process.
The above is the detailed content of How to obtain user UnionID, nickname, and avatar information in mini program development. 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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor
