How to log in using WeChat on PHP website
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.
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.
2. Official scene reference provided
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
4. Authorization to obtain access_token sequence diagram
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端跳转路径 ),
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; }
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.
The display effect is as follows:
Page jump after successful scanning and login
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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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.
