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

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

王雪芹
Release: 2020-05-02 11:36:07
Original
4495 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template