Table of Contents
1. Development instructions
1. Concept distinction
4. Summary
3. Typical problems
2. WeChat Open Platform Operation
1. Brief Guide
2. Official scene reference provided
3. Bind public account/mini program
4. Authorization to obtain access_token sequence diagram
3. Code implementation
1. Public file configuration
2. Core code
3. Front-end display
Home Backend Development PHP Tutorial How to log in using WeChat on PHP website

How to log in using WeChat on PHP website

May 12, 2018 pm 04:04 PM
php method Log in


Recently, the website development on the PC side requires WeChat authorized login. Considering that the mobile phone side has already gained a large number of WeChat user groups in the early stage, we are now thinking about integrating resources to meet the same needs. Data synchronization of WeChat users.

1. Development instructions

1. Concept distinction

Because you are exposed to the knowledge of WeChat development, you will inevitably be exposed to the use of OpenID and UnionID. The following is the official document of WeChat Introduction, please pay attention to the distinction:

  • After the follower interacts with the official account, the official account can obtain the follower’s OpenID (encrypted WeChat ID, each user The OpenID of each official account is unique. The OpenID of the same user is different for different official accounts). Official accounts can obtain basic user information based on OpenID through corresponding interfaces, including nickname, avatar, gender, city, language and time of attention.

  • Please note that if developers need to unify user accounts between multiple public accounts, or between public accounts and mobile applications, they need to go to the WeChat open platform (open.weixin. qq.com) After binding the official account, the UnionID mechanism can be used to meet the above needs.

To put it simply:

  • OpenID is the identification of an ordinary user and is unique to the current developer account. An OpenID corresponds to an official account.

  • UnionID is the user’s unified identification. For applications under a WeChat open platform account, the UnionID of the same user is unique.

2. Summary

has gone around this circle, that is to say, there is a difference between WeChat public account development and WeChat development platform development. If the same WeChat user logs in to a website using different platforms (such as PC, app, WeChat applet, etc.), the account needs to be bound, and the bound account cannot be distinguished by OpenID, but needs to be distinguished by UnionID.

3. Typical problems

Appendix A common design problem is mainly that the knowledge used before development is not comprehensive enough, which will have an impact on subsequent expansion. Of course, this is also a problem I encountered. I hope this can be a wake-up call for you.

2. WeChat Open Platform Operation

1. Brief Guide

According to the following needs, I chose "Website Application Development" Create and then apply for materials according to official instructions, which generally takes more than three days.
How to log in using WeChat on PHP website

After the application is created, it must also meet the requirements for obtaining interface permissions. Staff will proactively contact you, and it can usually be completed in one day.
How to log in using WeChat on PHP website

2. Official scene reference provided

How to log in using WeChat on PHP website

3. Bind public account/mini program

For To ensure that the UnionID corresponding to the WeChat user under the same development account is bound and used, the corresponding official account/service account needs to be bound in the list below. The document introduces that it generally must meet the WeChat payment function

How to log in using WeChat on PHP website

4. Authorization to obtain access_token sequence diagram

How to log in using WeChat on PHP website

3. Code implementation

In fact, the main time is spent on the early application operation above, but the actual code implementation is extremely simple. The following is my implementation method, please give me your feedback.

1. Public file configuration

It is customary to place the main configuration information in the configuration file, ‘\Application\Common\Conf\config.php’.

'WEIXIN_LOGIN' => array(        // 微信开放平台 使用微信帐号登录App或者网站 配置信息
        'OPEN_APPID' => 'wxbd961b2a6b7b2963', //应用 AppID
        'OPEN_APPSECRET' => 'e6xxxxxxxxxxxxxxxxxxxxe90',//应用 AppSecret
        'OPEN_CALLBACKURL' => 'http://www.52zhenmi.com/Home/Login/wxBack', //微信用户使用微信扫描二维码并且确认登录后,PC端跳转路径
    ),
Copy after login
2. Core code
public function wxIndex(){
        //--微信登录-----生成唯一随机串防CSRF攻击
        $state  = md5(uniqid(rand(), TRUE));        $_SESSION["wx_state"]    =   $state; //存到SESSION
        $callback = urlencode($this->callBackUrl);        'https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect';        $wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid="
                .$this->appID."&redirect_uri="
                .$callback."&response_type=code&scope=snsapi_login&state="
                .$state."#wechat_redirect";
        header("Location: $wxurl");
    }    public function wxBack(){
        if($_GET['state']!=$_SESSION["wx_state"]){            echo 'sorry,网络请求失败...';            exit("5001");
        }        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appID.'&secret='.$this->appSecret.'&code='.$_GET['code'].'&grant_type=authorization_code';        $arr = curl_get_contents($url);        //得到 access_token 与 openid
        $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['openid'].'&lang=zh_CN';        $user_info = curl_get_contents($url);        $this->dealWithWxLogin($user_info);
    }    /**
     * 根据微信授权用户的信息 进行下一步的梳理
     * @param $user_info
     */
    public function dealWithWxLogin($user_info){
        //TODO 数据处理
        var_dump($user_info);        die;
    }
Copy after login
3. Front-end display

According to the introduction of the official document, you can either directly access the authorized scanning interface or perform customized design. I guess I'm out of my mind, and the function of nested login and QR code has not been implemented for a long time, so I have to use the default jump here.

How to log in using WeChat on PHP website

The display effect is as follows:

How to log in using WeChat on PHP website

Page jump after successful scanning and login
How to log in using WeChat on PHP website

4. Summary

  • 1. According to the final implementation of the above function, the information of the logged-in user can be obtained, and the openID and UnionID can be stored in the database for later business Processing.

  • 2. My ability to explain below is limited. It is recommended to refer to the official development documents and the practical experience of Google seniors...

  • 3. I saw a good article online Article, recommended reference: Binding scheme for WeChat public account users and website users

Related recommendations:

Customize WeChat login code scanning style Solution

WeChat open platform development website application WeChat login introduction

How to quickly integrate WeChat login with PHP's laravel framework

The above is the detailed content of How to log in using WeChat on PHP website. 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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles