The method of implementing automatic login and registration function of micro-signal scanning code in PHP

Source: Internet
Author: User
Tags openid
This article mainly introduces the PHP implementation of the automatic Scan code login and registration function, combined with examples of PHP two-dimensional code recognition interface and related use skills, the need for friends can refer to the next

Development is now the programmer must master a basic technology, in fact, has been developed to know that the interface is very powerful to do it is very simple, here we look at an automatic login registration example.

PHP Scan Code PC-side Automatic Login Registration interface scope is Snsapi_userinfo, landing one is the website authorized landing, the other is the joint landing

Website Authorization 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

First, the link with a logo to generate two-dimensional code

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 make a fuss on state, Because state, what are you passing in, back there?

Can be used as an identity for servers and segments:

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::p ng ($value, False, $errorCorrectionLevel, $matrixPointSize);}}

At this point the QR code is generated and the state is identified, Phpqrcode can be downloaded at the end of the article, so we set the callback address Http://www.xxx.net/login/wcallback

Can be in the Wcallback method inside the data inserted user generation session, jump login, PC can set a few seconds Ajax request server, once acquired the state, that is, the implementation of the adjustment, the browser can close the window after processing, JS can be achieved:

Document.addeventlistener (' Weixinjsbridgeready ', function Onbridgeready () {weixinjsbridge.call (' CloseWindow ');}, FALSE);

You can also authorize the login success to jump to the service number following page:

Header ("location:weixin://profile/gh_a5e1959f9a4e"); Wcallback method to do processing login $code = $_get[' code ']; $state = $_get[' state ']; $setting = include Config_path. ' setting.php ' $appid = $setting [' Weixin '] [' AppID ']; $appsecret = $setting [' Weixin '] [' Appsecret '];if (Emptyempty ($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;//Turn 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 '//Turn 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 Download here do not provide you can Baidu search download

Magento Scan Code Site Automatic Login Example
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang= Zh_cn

After viewing the authorization interface Call (Unionid), it is not difficult to find the callback address, the user confirmed to log on to the PC can jump

Get the Unionid method

Public Function wcallbackaction () {$code = $_get[' code '), $state = $_get[' state '; $setting = include Config_path. ' setting.php '; $appid = $setting [' Weixin '] [' AppID ']; $appsecret = $setting [' Weixin '] [' Appsecret '];if (Emptyempty ($ 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 ' 

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.