Home PHP Framework ThinkPHP [Dry information] ThinkPHP6 docking WeChat scan code to log in

[Dry information] ThinkPHP6 docking WeChat scan code to log in

May 02, 2020 am 11:24 AM
thinkphp WeChat

In recent years, there are more and more scenarios where WeChat is used to log in on Internet websites. According to statistics, in 2020, the number of WeChat in the world reached 1.1 billion. It is true that WeChat, a useful social tool, can be used by anyone as young as a primary school student or as large as your seventh aunt or uncle. Many people may not have QQ, but they must have WeChat. . Therefore, WeChat login is an essential work skill for programmers.

Scan the WeChat QR code to log in and connect to ThinkPHP6. Without further ado, just get on the bus.

1. Prepare information:

1. Visit https://open.weixin.qq.com/ and register an account.

2. Developer certification: Enterprise.

3. Create a website application: The website domain name must be registered (second-level domain names can be used), obtain the corresponding AppID and AppSecret, apply for WeChat login and pass the review.

2. Steps to log in to WeChat:

First, take a look at the step instructions given on the WeChat official website: https://developers.weixin .qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html

1. A third party initiates a WeChat authorized login request. After the WeChat user allows authorization of the third-party application, WeChat will pull Start the application or redirect to a third-party website, and bring the authorization temporary ticket code parameter;

2. Add AppID and AppSecret through the code parameter, and exchange for access_token through the API;

3. Make interface calls through access_token to obtain users' basic data resources or help users implement basic operations.

3. Access to WeChat login practical link:

1. Place the WeChat login icon and add Link.

For example, link to www.a,com/index/user/weixindenglu. Let's take a look at the weixindenglu method code.

public function weixindenglu(){
   $appid='wx868f988d79a4f2bb';
   $redirect_uri=urldecode('http://www.dongpaiweb.cn/index/index/weixin.html');
   $url='https://open.weixin.qq.com/connect/qrconnect?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect';
        header("location:".$url);
}
Copy after login

At this time, when we click on the WeChat icon, the QR code scanning interface will appear. Take out your phone and quickly scan the QR code on WeChat.

(Note: $redirect_uri is our callback address, which means the processing address after the user scans the WeChat code).

2. Obtain the user's code.

After scanning the QR code on WeChat, it jumps to the callback address weixin method defined above. Let’s take a look at the weixin method code:

    public function weixin(){
        $code=input('get.code');
    }
Copy after login

code is very simple to obtain. Let’s take a look at the printing effect:

[Dry information] ThinkPHP6 docking WeChat scan code to log in

3. Obtain Access Token and openid. We are Continue to add code to the weixin() method:

public function weixin(){
        $code=input('get.code');
        $appid='wx868f988d79a4f25b';
        $appsecret='82b426f2882b6a1398b8312cc1de037b';
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        
        //json对象变成数组
        $res=json_decode(file_get_contents($url),true);
        $access_token=$res['access_token'];
        $openid=$res['openid'];

    }
Copy after login

In this way, we have obtained the access_token and openid. Let’s take a look at the printing effect:

[Dry information] ThinkPHP6 docking WeChat scan code to log in

5. To obtain all user information, we continue to add code in the weixin() method:

public function weixin(){
        $code=input('get.code');
        $appid='wx868f988d79a4f25b';
        $appsecret='82b426f2882b6a1398b8312cc1de037b';
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        
        //json对象变成数组
        $res=json_decode(file_get_contents($url),true);
        $access_token=$res['access_token'];
        $openid=$res['openid'];

        $urlyonghu='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid;
        $user=json_decode(file_get_contents($urlyonghu),true);
        print_r($user);
    }
Copy after login

In this way, we obtain the user’s nickname, address, avatar and other information. Let’s see the printing effect:

[Dry information] ThinkPHP6 docking WeChat scan code to log in

After we obtain the user’s WeChat information, we can organize the data and put it into the database.

If it is the first time for the user to log in, we can set up the interface for binding the mobile phone number. After binding the mobile phone number, the registration is successful. If we detect that the mobile phone number has been bound, it means that the login is successful and jumps to the success interface.

The above are the steps for connecting ThinkPHP6 to WeChat scan code to log in. Get a salary increase and promotion, get this skill quickly!

The above is the detailed content of [Dry information] ThinkPHP6 docking WeChat scan code to log in. For more information, please follow other related articles on the PHP Chinese website!

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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem of cURL error 77 when connecting to Elasticsearch 8 using ThinkPHP6 and elasticsearch-php clients? How to solve the problem of cURL error 77 when connecting to Elasticsearch 8 using ThinkPHP6 and elasticsearch-php clients? Mar 31, 2025 pm 11:36 PM

Using the ThinkPHP6 framework combined with elasticsearch-php client to operate Elasticsearch...

ThinkPHP6 routing: How to completely obtain URL parameters containing special characters such as Chinese? ThinkPHP6 routing: How to completely obtain URL parameters containing special characters such as Chinese? Apr 01, 2025 pm 02:51 PM

ThinkPHP6 routing parameters are processed in Chinese and complete acquisition. In the ThinkPHP6 framework, URL parameters containing special characters (such as Chinese and punctuation marks) are often processed...

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

What is the difference between H5 page production and WeChat applets What is the difference between H5 page production and WeChat applets Apr 05, 2025 pm 11:51 PM

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? Apr 01, 2025 pm 10:48 PM

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

How to solve the problem of JS resource caching in enterprise WeChat? How to solve the problem of JS resource caching in enterprise WeChat? Apr 04, 2025 pm 05:06 PM

Discussion on the JS resource caching issue of Enterprise WeChat. When upgrading project functions, some users often encounter situations where they fail to successfully upgrade, especially in the enterprise...

See all articles