It is still relatively simple to automatically log on to a website by scanning the QR code. As long as you know the principles of each other, you can do so. Let's look at two examples, I believe you have read the two examples and you will surely know how to do it. It is still relatively simple to automatically log on to a website by scanning the QR code. As long as you know the principles of each other, you can do so. Let's look at two examples, I believe you have read the two examples and you will surely know how to do it.
Script ec (2); script
Magento scan the QR code to automatically log on to the website
The case is similar to the joint login practice. The joint login introduction is as follows:
Https://open.weixin.qq.com/cgi-bin/showdocument? Action = dir_list & t = resource/res_list & verify = 1 & lang = zh_CN
View the API call after authorization (UnionID), it is not difficult to fill in the callback address, the user can jump to the pc
Obtain UnionID
Public function wcallbackAction (){
$ Code = $ _ GET ['code'];
$ State = $ _ GET ['state'];
$ Setting = include CONFIG_PATH. 'setting. php ';
$ Appid = $ setting ['weixin'] ['appid '];
$ Appsecret = $ setting ['weixin'] ['appsecret'];
If (empty ($ code) $ this-> showMessage ('authorization failed ');
Try {
$ Token_url = 'https: // api.weixin.qq.com/sns/oauth2/access_token? Appid = '. $ appid.' & secret = '. $ appsecret.' & code = '. $ code.' & grant_type = authorization_code ';
$ Token = json_decode ($ this-> https_request ($ token_url ));
} Catch (Exception $ e)
{
Print_r ($ e );
}
If (isset ($ token-> errcode )){
Echo 'error: '. $ token-> errcode;
Echo'
Error message: '. $ token-> errmsg;
Exit;
}
$ Access_token_url = 'https: // api.weixin.qq.com/sns/oauth2/refresh_token? Appid = '. $ appid.' & grant_type = refresh_token & refresh_token = '. $ token-> refresh_token;
// Convert to object
$ Access_token = json_decode ($ this-> https_request ($ access_token_url ));
If (isset ($ access_token-> errcode )){
Echo 'error: '. $ access_token-> errcode;
Echo'
Error message: '. $ access_token-> errmsg;
Exit;
}
$ User_info_url = 'https: // api.weixin.qq.com/sns/userinfo? Access_token = '. $ access_token-> access_token.' & openid = '. $ access_token-> openid.' & lang = zh_CN ';
// Convert to object
$ User_info = json_decode ($ this-> https_request ($ user_info_url ));
If (isset ($ user_info-> errcode )){
Echo 'error: '. $ user_info-> errcode;
Echo'
Error message: '. $ user_info-> errmsg;
Exit;
}
// Print user information
// Echo'
';
// print_r($user_info);
// echo '
';
// Obtain the unionid
$ Uid = $ user_info-> unionid;
}
// User operation processing is divided into the next login and the First Login
$ SQL = "select h_user_id from dtb_user_binded as t1 left join dtb_user_weixin as t2 on t1.u _ id = t2.id where t1.u _ type = '".
User: $ arrUtype ['weixin _ num_t ']. "' and t2.openid = '$ user_info-> unionid '";
$ H_user_id = Core_Db: getOne ($ SQL );
If (! Empty ($ h_user_id) {// log on again with the weixin number
} {// The First Login to this weixin number
}
Php scan code pc terminal Automatic Login Registration
The scope interface used is snsapi_userinfo. One is webpage-authorized login, and the other is joint login.
Web authorized login: http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
Joint login: https://open.weixin.qq.com/cgi-bin/frame? T = home/web_tmpl & lang = zh_CN
I. Generate a QR code with a link ID
For example, the link is https://open.weixin.qq.com/connect/oauth2/authorize? Appid = '. $ appid. '& redirect_uri = '. $ url. '& response_type = code & scope = snsapi_userinfo & state = 1 # wechat_redirect' We can post on the state, because the state is passed in to what side to return
Can be used as a server and segment identifier
Public function creatqrAction (){
If ($ _ GET ['app']) {
$ Wtoken = $ _ COOKIE ['wtoken'];
$ Postdata = $ _ SESSION ['w _ state'];
If ($ wtoken ){
$ Postdata = $ wtoken;
}
Include CONFIG_PATH. 'phpqrcode/'. 'phpqrcode. php ';
$ Sh = $ this-> shar1 ();
$ Value = "https://open.weixin.qq.com/connect/oauth2/authorize? Appid = wx138697ef383a9167 & redirect_uri = http://www.xxx.net/login/wcallback&response_type=code&scope=snsapi_userinfo&state= ". $ postdata." & connect_redirect = 1 # wechat_redirect ";
$ ErrorCorrectionLevel = "L ";
$ MatrixPointSize = "5 ";
QRcode: png ($ value, false, $ errorCorrectionLevel, $ matrixPointSize );
}
}
The generated QR code state is the identifier, phpqrcode can be downloaded at the end of the article, so we set the callback address http://www.xxx.net/login/wcallback
In the wcallback method, you can insert data into the user to generate a session and jump to the login. On the pc side, you can set the ajax request server for several seconds.
State, that is, to achieve adjustment, the browser can close the window after processing, js can achieve
Document. addEventListener ('weixinjsbridgeready', function onBridgeReady (){
WeixinJSBridge. call ('closewindow ');
}, False );
You can also authorize successful login to jump to the service number follow page
Header ("Location: weixin: // profile/gh_a5e1959f9a4e ");
Wcallback method for processing Login
$ Code = $ _ GET ['code'];
$ State = $ _ GET ['state'];
$ Setting = include CONFIG_PATH. 'setting. php ';
$ Appid = $ setting ['weixin'] ['appid '];
$ Appsecret = $ setting ['weixin'] ['appsecret'];
If (empty ($ code) $ this-> showMessage ('authorization failed ');
Try {
$ Token_url = 'https: // api.weixin.qq.com/sns/oauth2/access_token? Appid = '. $ appid.' & secret = '. $ appsecret.' & code = '. $ code.' & grant_type = authorization_code ';
$ Token = json_decode ($ this-> https_request ($ token_url ));
} Catch (Exception $ e)
{
Print_r ($ e );
}
If (isset ($ token-> errcode )){
Echo 'error: '. $ token-> errcode;
Echo'
Error message: '. $ token-> errmsg;
Exit;
}
$ Access_token_url = 'https: // api.weixin.qq.com/sns/oauth2/refresh_token? Appid = '. $ appid.' & grant_type = refresh_token & refresh_token = '. $ token-> refresh_token;
// Convert to object
$ Access_token = json_decode ($ this-> https_request ($ access_token_url ));
If (isset ($ access_token-> errcode )){
Echo 'error: '. $ access_token-> errcode;
Echo'
Error message: '. $ access_token-> errmsg;
Exit;
}
$ User_info_url = 'https: // api.weixin.qq.com/sns/userinfo? Access_token = '. $ access_token-> access_token.' & openid = '. $ access_token-> openid.' & lang = zh_CN ';
// Convert to object
$ User_info = json_decode ($ this-> https_request ($ user_info_url ));
If (isset ($ user_info-> errcode )){
Echo 'error: '. $ user_info-> errcode;
Echo'
Error message: '. $ user_info-> errmsg;
Exit;
}
// Print user information
// Echo'
';
// print_r($user_info);
// echo '
';