资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在php项目中实现一个微信扫码登陆功能-创新互联

这篇文章将为大家详细讲解有关怎么在php项目中实现一个微信扫码登陆功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

成都创新互联公司是一家专注于成都网站制作、做网站与策划设计,松原网站建设哪家好?成都创新互联公司做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:松原等地区。松原做网站价格咨询:18980820575

一、首先把微信链接带个标识生成二维码


比如链接为 https://open.weixin.qq.com/connect/oauth3/authorize?appid='.$appid.'&redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'  我们可以在state上做文章,因为state你传入什么微信那边返回什么

可以作为服务器与微信段的一个标识:

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

此时生成了二维码 state是标识,phpqrcode可以在文章末尾下载,这样我们设置了回调地址/tupian/20230522/pp就可以在wcallback方法里面处理数据 插入用户 生成session,跳转登陆,pc端可以设置几秒钟ajax请求服务器,一旦获取到了state,即实现调整,微信浏览器里处理完后可以关闭窗口,微信js可实现:

document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
WeixinJSBridge.call('closeWindow');}, false);

也可以授权登陆成功后跳转到微信服务号关注页面:

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 = '/tupian/20230522/access_token
$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 = '/tupian/20230522/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 = '/tupian/20230522/userinfo
//转成对象
$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类库下载在此不提供各位可以百度搜索下载

magento微信扫码网站自动登录的例子
/tupian/20230522/showdocument>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 = '/tupian/20230522/access_token; $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 = '/tupian/20230522/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 = '/tupian/20230522/userinfo; //转成对象 $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 '
'; //获取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号第一次登录 }

关于怎么在php项目中实现一个微信扫码登陆功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


本文题目:怎么在php项目中实现一个微信扫码登陆功能-创新互联
地址分享:http://cdkjz.cn/article/dhhosj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220