Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Wechat Code scanning Login by php

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces php how to achieve Wechat code scan login, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Php to achieve Wechat code login method: 1, instantiate an object through js; 2, define a div in html and include a QR code; 3, instantiate in $(document). Ready ().

This article operating environment: windows10 system, php 7, thinkpad T480 computer.

Wechat has become an indispensable part of our daily life. In order to make it more convenient for more users to use Wechat and related products, Wechat scan function is becoming more and more common. So what should we do if we want to implement this function ourselves?

Before giving the specific implementation code, let's analyze the login process of Wechat scan code.

First of all, we have to display the QR code on the page. The QR code has expiration time and failure state. Once you scan the QR code or do not scan the QR code on the page for a certain period of time, then the QR code is invalid. The Wechat official website provides us with two ways to display the QR code, one is that the background sends a request to return a new page, and the other is that the instantiated QR code of the front-end js is embedded in our own page. Obviously, the first method is relatively simple and convenient, but both methods will be used in actual projects, so let's explain both ways.

1. The backend sends a request to get the code scanning page returned by Wechat.

$redirect_uri= "http:// the method of handling code scanning events under your Wechat open platform binding domain name"; $redirect_uri=urlencode ($redirect_uri); / / the callback requires url encoding $appID= "your appid"; $scope= "snsapi_login"; / / for the time being, Wechat only supports this value / / is ready to send a request to Wechat $url = "https://open.weixin.qq.com/connect/qrconnect?appid=". $appID. "& redirect_uri=". $redirect_uri. "& response_type=code&scope=". $scope. "& state=STATE#wechat_redirect"; / / the result returned by the request (actually a string of html) $result = file_get_contents ($url); / / replace the src of the picture to display the QR code $result = str_replace ("/ connect/qrcode/", "https://open.weixin.qq.com/connect/qrcode/", $result); return $result; / / return the page

This will return a page like this, and call $redirect_uri after scanning.

2. Embedded JS displays:

Here, you can instantiate an object through the js side. First, add the following js file to the tag

Secondly, define a div containing QR code in html

Finally, instantiate it in $(document) .ready ():

$(document) .ready (function () {var obj = new WxLogin ({id: "login_container", / / div's id appid: "your appid", scope: "snsapi_login", / / write dead redirect_uri:encodeURI ("your way to handle code scanning events"), state: ", style:" black " / / QR code black and white style href: "https:// the css file under a domain name"}) });

Note that the css file pointed to in href can only be referenced under the https protocol, otherwise the default style will be on the page (the display is a large QR code, you can't adjust the size and location of the QR code, it's too painful). The last part of the page looks like this, and the QR code here is probably only 140px:

All right, the QR code appears on the page, and then we will roughly talk about the logic of code scanning, the whole process is roughly divided into 5 steps:

After these five steps, you will get all the information of the scanning user, and then write the code logic you need (such as redirect or login), as shown in the code:

/ callback public function codeinfo () {$code = $_ GET ["code"]; $appid = "your appid"; $secret = "your secret"; if (! empty ($code)) / have code {/ / get access_token + openid $url= "https://api.weixin.qq.com/sns/oauth3/access_token?appid=" through code. $appid. & secret=. $secret. & code=. $code. & grant_type=authorization_code; $jsonResult = file_get_contents ($url); $resultArray = json_decode ($jsonResult, true); $access_token = $resultArray ["access_token"]; $openid = $resultArray ["openid"] / / get all the user's information through access_token + openid, and the results are all stored in $infoArray, and then write your own code logic $infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=". $access_token. & openid=. $openid; $infoResult = file_get_contents ($infoUrl); $infoArray = json_decode ($infoResult, true);}}

I believe that after writing the above code, you have already made it very clear to scan the login process, in fact, it is essentially the cooperative call of multiple Wechat interfaces.

Thank you for reading this article carefully. I hope the article "how to achieve Wechat Code scanning login" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report