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

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

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');
    }

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'];

    }

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

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
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
What Are the Key Features of ThinkPHP's Built-in Testing Framework?What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Advanced Features of ThinkPHP's Dependency Injection Container?What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

How to Use ThinkPHP for Building Real-Time Collaboration Tools?How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PM

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

See all articles

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

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),