WeChat open platform provides the website scan code login interface, used to obtain the user’s basic information (avatar, nickname) convenient website quickly access to WeChat login, quick login. You need to use the login interface, and you need to be a WeChat open platform certified developer (300 yuan) to get the access to this interface.

Preparation:

1, prepare APPID, APPSECRET 2, prepare interface address 3, prepare REDIRECT_URI

Get the code interface

https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state= STATE#wechat_redirect

Get the ACESS_TOKEN, OpenID interface

https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

Interface for obtaining user information:

https://api.weixin.qq.com/sns/userinfo?access_token=access_token&openid=openid

Process:

1, access CODE 2, access access_token 3, access user information

Operation:

1. Request CODE

Parameters that

By interface address, concatenate the above parameters can be accessed

https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri= here to fill out redirect_uri & the payload = code&scope = SCOPE&st ate=STATE#wechat_redirect

Redirect_uri means that this is the address that you click on to scan the code, and the address is returned to you with two parameters, code and state.

State indicates the state used to hold the request and callback, authorizing the request to be brought back to a third party as is. This parameter can be used to prevent CSRF attacks (cross-site request forgery attacks). It is recommended that third parties bring this parameter. It can be set to a simple random number plus session for verification.

You can generate a random string by yourself. For easy learning, I will use a timestamp to generate a simple MD5 encryption

<? php $data = time(); $state = MD5($data); ? >

Such as your redirect_uri is http://www.baidu.com/login.php, then scan code, will jump address is as follows.

http://www.baidu.com/login.php?code= to generate code&state = generating state

Of course, redirect_uri requires urlEncode encoding.

<? php $redirect_uri = urlEncode("http://www.baidu.com/login.php"); ? >

The final access link to get CODE looks like this:

<? PHP $appid = "fill in your appid "; $redirect_uri = UrlEncode("http://www.baidu.com/login.php"); $data = time(); $state = MD5($data); // Jump to page echo "<script>location.href=\"https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$redirect_uri&response_t ype=code&scope=snsapi_login&state=$state#wechat_redirect\"; </script>"; ? >

Then I jump to a page where I scan the code:

2. Get the access_token and openID

Use curl to make a request to the interface

<? PHP // get code from redirect_uri $code = $_GET["code"]; $appid = "Fill in your list "; $secret = ""; // Get the access_token and openID $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code" ; function post($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $rst = curl_exec($ch); curl_close($ch); return $rst; $result = POST ($url); $arr = json_decode($result,true); $result = json_decode($result,true); $openid = $arr['openid']; $openid = $arr['openid']; $token = $arr['access_token']; ? >

3. Obtain user information

<? PHP / / there is then the above code / / get the user information needs openid and access_token $getinfourl = / / get the user information "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid"; function getinfo($getinfourl) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $getinfourl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $rst = curl_exec($ch); curl_close($ch); return $rst; $info_result = getInfo ($infoInfo); Echo $info_result; echo $info_result; $info_arr = json_decode($info_result,true); $nickname = $info_arr['nickname']; $headimgurl = $info_arr['headimgurl']; Echo "<img SRC =\"$headimgurl\"/>"; echo "<h2>$nickname<h2>"; ? >

The complete code

code.php

<? PHP $appid = "fill in "; $redirect_uri = UrlEncode("http://www.baidu.com/login.php"); $data = time(); $state = MD5($data); echo "<script>location.href=\"https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$redirect_uri&response_t ype=code&scope=snsapi_login&state=$state#wechat_redirect\"; </script>"; ? >

login.php

<! DOCTYPE HTML > < HTML > <head> <title> </title> <style type="text/css"> *{margin:0px; padding: 0px; } #headimg{ width: 180px; height: 180px; margin:100px auto 10px; border-radius: 100%; } #headimg img{ width: 180px; height: 180px; border-radius: 100%; } h2{ text-align: center; } p{ text-align: center; font-size: 38px; font-weight: bold; margin-top: 20px; } </style> </head> <body> </body> </html> <? php $code = $_GET["code"]; $appid = "Fill in your list "; $secret = ""; // Get the access_token and openID $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code" ; function post($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $rst = curl_exec($ch); curl_close($ch); return $rst; $result = POST ($url); $arr = json_decode($result,true); $result = json_decode($result,true); $openid = $arr['openid']; $token = $arr['access_token']; / / get the user information $getinfourl = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid"; function getinfo($getinfourl) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $getinfourl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $rst = curl_exec($ch); curl_close($ch); return $rst; $info_result = getInfo ($infoInfo); Echo $info_result; echo $info_result; $info_arr = json_decode($info_result,true); $nickname = $info_arr['nickname']; $headimgurl = $info_arr['headimgurl']; $errcode = $info_arr['errcode']; If ($errcode == "41001") {if ($errcode == "41001") {echo "<p "; Echo "<p><a href=\"code.php\"> </a><p>"; }else{ echo "<div id=\"headimg\"><img src=\"$headimgurl\"/></div>"; echo "<h2>$nickname<h2>"; Echo "<p> login successfully <p>"; }? >

The DEMO:Click to view

Learn to communicate WeChat: FACE6009 https://likeyunba.com