Thinkphp3.2 how to use wechat to scan the qr code to achieve the function of web page login
with the continuous development of chat tools, wechat is now ahead of the many chat tools, which means that basically everyone’s mobile phones will have wechat software, so scanning the qr code to log in to the web page is better than sina. QQ third-party platforms such as login are more widely used.
the principle of realizing login on the web page of wechat scan code is to use the wechat login function of the official account page, but some codes will be added and changed to make the mobile phone PC the end can form a linkage of data. let’s focus on the content of changing and adding code.
the first step is to generate a qr code, as shown in the figure below:
//random string for conjunction PC media with mobile phone
$rand_code = 'M'.time().rand('100001','999999');
//the address returned after login
$REDIRECT_URI = 'domain name /Weixin/login_return?rand_code='.$rand_code;
//wechat login address, also qr code address
$scope='snsapi_userinfo';//indicates that authorization is required
$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state=1#wechat_redirect';//login verification
vendor("phpqrcode.phpqrcode");//call the qr code class
$level = 'L';// point size: 1 to 10, just use it on mobile phone 4
$size = 7;/* 二维码的大小,下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName replace the second parameter false*/
$fileName ='Uploads/qcode/2.png'; // the generated file name
$QRcode = new \QRcode();
$QRcode->png($url, $fileName, $level, $size);//generate qr code
$this->qrcode = .'/'.$fileName;//qr code path
$this->rand_code = $rand_code;//pass random numbers to the page where you scan the code
如上所示,$url就是二维码的内容,扫码后手机就浏览$url,手机确认授权登录后,就会返回$REDIRECT_URI the address, the method is as follows:
function login_return(){
$rand_code = I('get.rand_code','');//PC the generated random number
$appid = C('APPID');
$secret = C('SECRET');
$code = $_GET["code"];
//get the user's access_token and openid
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
$data = fopen($get_token_url, "rb");
$data = stream_get_contents($data);
@fclose($data);
$json_obj = json_decode($data,true);
//according to openid and access_token query user information
$access_token = $json_obj['access_token'];
$openid = $json_obj['openid'];
$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
$data2 = fopen($get_user_info_url, "rb");
$data2 = stream_get_contents($data2);
@fclose($data2);
//analysis json
$info = json_decode($data2,true); //$info that is an array of user information
}
the above is the code, principle: after scanning the code on your mobile phone, you can get the user's information. if you have a nickname, avatar, region, etc., the most important thing is openid,according to openid查询会员表有没有存在,如果存在则将随机数$rand_code update to membership form member of rand_code if it does not exist, add a line of member information. the added content is the user's nickname and avatar. openid, rand_code, because rand_code it has been saved in the membership list, so PC the web page uses a function executed regularly to check whether the membership table exists every few seconds. rand_code, if there is, the login will be successful and the information of the member is obtained. PC extremely JS as follows:
the function of wechat login is relatively simple, mainly to know how the principle is implemented.

the above is shared by suyou.com with everyone." Thinkphp3.2 how to use wechat to scan the qr code to realize the function of web page login ", thank you very much for your patience to read this article. we will provide you with more information on reference, use or learning and communication. we can also provide you with:enterprise website construction, website imitation, website copying, imitation site, foreign trade website construction, foreign trade website construction, company official website productionfor services, our company serves customers with the service concept of "integrity, professionalism, pragmatism and innovation". if you need cooperation, please scan the code to consult, and we will serve you sincerely.
TAG label: