_php example of realizing the automatic login and registration function of micro-scan code in PHP

Source: Internet
Author: User
Tags openid

This article describes the PHP implementation of micro-scan code automatic landing and registration function. Share to everyone for your reference, specific as follows:

Micro-credit development is now a programmer must master a basic technology, in fact, have done micro-letter development are aware of the micro-letter interface is very powerful to do it is also very simple, here we look at a micro-letter automatic Landing Registration example.

PHP micro-Scan Code PC-side automatic landing register with the interface scope is snsapi_userinfo, micro-letter landing is a Web page authorization landing, and the other is a micro-letter joint landing

Web page Authorization Login: http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

Micro-letter joint landing: Https://open.weixin.qq.com/cgi-bin/frame?t=home/web_tmpl&lang=zh_CN

First, the micro-letter link with a logo to generate a 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 over the state, Because of the state, what kind of micro-trust you're going to return?

Can be used as an identifier for a server and a micro-segment:

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);
}


Two-dimensional code is generated at this time the state is the identity, 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 processing data Insert user generation session, jump landing, PC can set a few seconds Ajax request server, once acquired to state, that is, realize adjustment, micro-letter browser can be closed after processing, micro-letter JS can be achieved:

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

You can also authorize the landing after the successful jump to the micro-mail service number of attention page:

Header ("location:weixin://profile/gh_a5e1959f9a4e");
Wcallback method to do processing landing $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;
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 Download does not provide you can Baidu search download

Magento Micro-Scan code website 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 out the callback address, the user confirmed the landing PC can jump

Get 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 '  

For more information on PHP related content readers can view the site topics: "PHP micro-credit Development Skills summary", "PHP coding and transcoding Operation skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

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.