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 use php to realize Wechat login

2025-04-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use php to achieve Wechat login". In daily operation, I believe many people have doubts about how to use php to achieve Wechat login. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to use php to achieve Wechat login". Next, please follow the editor to study!

The method of realizing Wechat login by php is as follows: 1. With the consent and authorization of the user, the code;2 is obtained, the web page authorization access_token;3 is obtained through code, and the user information is obtained.

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

It is not difficult to log in to Wechat using php. It can be simply divided into three steps, as shown below:

Step 1: the user agrees to the authorization and obtains the code

/ / Wechat login public function wxlogin () {$appid = ""; $secret = ""; $str= "http://***.***.com/getToken"; $redirect_uri=urlencode ($str) / / obtain access_token + openid $url= "https://open.weixin.qq.com/connect/oauth3/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"; header (" Location: ". $url);}

Step 2: exchange code for web page authorization access_token

Public function getToken () {$code = $_ GET ["code"]; $appid = ""; $secret= ""; / / get access_token + openid $url= "https://api.weixin.qq.com/sns/oauth3/access_token?appid=".$appid." & secret= "through code. $secret. & code=. $code. & grant_type=authorization_code; $jsonResult = $this- > https_request ($url); $resultArray = json_decode ($jsonResult, true); $access_token = $resultArray ["access_token"]; $openid = $resultArray ["openid"] / / the third step is to get user information / / get all the user information through access_token + openid, 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.'&lang=zh_CN'; $infoResult = $this- > https_request ($infoUrl); $infoArray = json_decode ($infoResult, true); if ($infoArray ['unionid']) {}}

Attaching: methods used in the code

/ / An highlighted blockfunction https_request ($url, $data = null) {$curl = curl_init (); curl_setopt ($curl, CURLOPT_URL, $url); curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (! empty ($data)) {curl_setopt ($curl, CURLOPT_POST, 1) Curl_setopt ($curl, CURLOPT_POSTFIELDS, $data);} curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec ($curl); curl_close ($curl); return $output;} this, on "how to use php to achieve Wechat login" study is over, I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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