Example of php WeChat Automatic Login registration by scanning the code-PHP source code

Source: Internet
Author: User
Development is now a basic technology that programmers must master. In fact, all developers know that interfaces are very powerful and simple, let's take a look at an example of Automatic Login registration. Development is now a basic technology that programmers must master. In fact, all developers know that interfaces are very powerful and simple, let's take a look at an example of Automatic Login registration.

Script ec (2); script


Php scan code the interface used for automatic logon and registration on the pc side is snsapi_userinfo. One is webpage-authorized logon, and the other is joint logon.

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 '

'

Phpqrcode class library downloading is not provided here. You can search and download it through Baidu.

Magento scan the code to automatically log on to the website

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

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.