This article mainly introduces the automatic login and registration function of WeChat code scanning in php, and analyzes the php WeChat QR code recognition interface and related usage skills in the form of examples. Friends in need can refer to
WeChat Development is now a basic technology that programmers must master. In fact, anyone who has done WeChat development knows that the WeChat interface is very powerful and very simple to implement. Here we look at an example of WeChat automatic login and registration.
php The interface scope used for automatic login and registration by WeChat scanning code on PC is snsapi_userinfo. One WeChat login is web page authorization login, and the other is WeChat joint login.
Web page authorization login: http ://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
WeChat joint login: https://open.weixin.qq.com/cgi-bin/frame?t=home/ web_tmpl&lang=zh_CN
1. First, add a logo to the WeChat link to generate a QR 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 about state, because what state you pass in to WeChat will return what
can be used as an identifier between the server and the WeChat 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::png($value, false, $errorCorrectionLevel, $matrixPointSize); } }
The QR code state is generated at this time and is the identifier. phpqrcode can be used in the article. Download at the end, so that we set the callback address http://www.xxx.net/login/wcallback
, we can process the data in the wcallback method, insert the user-generated session, jump to login, and several settings can be set on the PC side. Ajax request to the server in seconds. Once the state is obtained, the adjustment is implemented. After processing in the WeChat browser, the window can be closed. WeChat js can achieve:
document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() { WeixinJSBridge.call('closeWindow');}, false);
You can also authorize and jump to the WeChat service account follow page after successful login:
header("Location: weixin://profile/gh_a5e1959f9a4e"); wcallback方法做处理登陆 $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('授权失败'); 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 '错误:'.$token->errcode; echo '错误信息:'.$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; //转成对象 $access_token = json_decode($this->https_request($access_token_url)); if (isset($access_token->errcode)) { echo '错误:'.$access_token->errcode; echo '错误信息:'.$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' //转成对象 $user_info = json_decode($this->https_request($user_info_url)); if (isset($user_info->errcode)) { echo '错误:'.$user_info->errcode; echo '错误信息:'.$user_info->errmsg; exit; } //打印用户信息 // echo '' // print_r($user_info); // echo ''
phpqrcode class library download is not available here. You can search and download it on Baidu
Example of automatic login for magento WeChat QR code scanning website
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN
Check the post-authorization interface call (UnionID). It is not difficult to find that filling in the callback address and the user confirms logging in to the PC will jump to
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('授权失败'); 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 '<h1>错误:</h1>'.$token->errcode; echo '<br/><h2>错误信息:</h2>'.$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; //转成对象 $access_token = json_decode($this->https_request($access_token_url)); if (isset($access_token->errcode)) { echo '<h1>错误:</h1>'.$access_token->errcode; echo '<br/><h2>错误信息:</h2>'.$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'; //转成对象 $user_info = json_decode($this->https_request($user_info_url)); if (isset($user_info->errcode)) { echo '<h1>错误:</h1>'.$user_info->errcode; echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg; exit; } //打印用户信息 // echo '<pre class="brush:php;toolbar:false">'; // print_r($user_info); // echo ''; //获取unionid $uid=$user_info->unionid; } //用户操作处理 分为再次登录和第一次登陆 $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(!emptyempty($h_user_id)){//该weixin号再次登录 }{//该weixin号第一次登录 }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
Detailed explanation of data encryption and decryption using PHP symmetric encryption function
Principle and implementation of PHP image recognition technology
php PATH_SEPARATOR determines the current server system type instance
The above is the detailed content of How to implement the automatic login and registration function of WeChat code scanning in PHP. For more information, please follow other related articles on the PHP Chinese website!