Home >Backend Development >PHP Tutorial >How to implement the automatic login and registration function of WeChat code scanning in PHP

How to implement the automatic login and registration function of WeChat code scanning in PHP

墨辰丷
墨辰丷Original
2018-06-01 10:43:091836browse

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 &#39;<h1>错误:</h1>&#39;.$token->errcode;
echo &#39;<br/><h2>错误信息:</h2>&#39;.$token->errmsg;
exit;
}
$access_token_url = &#39;https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=&#39;.$appid.&#39;&grant_type=refresh_token&refresh_token=&#39;.$token->refresh_token;
//转成对象
$access_token = json_decode($this->https_request($access_token_url));
if (isset($access_token->errcode)) {
echo &#39;<h1>错误:</h1>&#39;.$access_token->errcode;
echo &#39;<br/><h2>错误信息:</h2>&#39;.$access_token->errmsg;
exit;
}
$user_info_url = &#39;https://api.weixin.qq.com/sns/userinfo?access_token=&#39;.$access_token->access_token.&#39;&openid=&#39;.$access_token->openid.&#39;&lang=zh_CN&#39;;
//转成对象
$user_info = json_decode($this->https_request($user_info_url));
if (isset($user_info->errcode)) {
echo &#39;<h1>错误:</h1>&#39;.$user_info->errcode;
echo &#39;<br/><h2>错误信息:</h2>&#39;.$user_info->errmsg;
exit;
}
//打印用户信息
// echo &#39;<pre class="brush:php;toolbar:false">&#39;;
// print_r($user_info);
// echo &#39;
'; //获取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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn