php实现微信扫码自动登陆与注册功能
微信(小程序)  /  管理员 发布于 6年前   133
本文实例讲述了php实现微信扫码自动登陆与注册功能。分享给大家供大家参考,具体如下: 微信开发已经是现在程序员必须要掌握的一项基本的技术了,其实做过微信开发的都知道微信接口非常的强大做起来也非常的简单,这里我们一起来看一个微信自动登陆注册的例子. php 微信扫码 pc端自动登陆注册 用的接口scope 是snsapi_userinfo,微信登陆一个是网页授权登陆,另一个是微信联合登陆 网页授权登陆:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html 微信联合登陆:https://open.weixin.qq.com/cgi-bin/frame?t=home/web_tmpl&lang=zh_CN 一、首先把微信链接带个标识生成二维码 比如链接为 https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect' 我们可以在state上做文章,因为state你传入什么微信那边返回什么 可以作为服务器与微信段的一个标识: 此时生成了二维码 state是标识,phpqrcode可以在文章末尾下载,这样我们设置了回调地址http://www.xxx.net/login/wcallback 就可以在wcallback方法里面处理数据 插入用户 生成session,跳转登陆,pc端可以设置几秒钟ajax请求服务器,一旦获取到了state,即实现调整,微信浏览器里处理完后可以关闭窗口,微信js可实现: 也可以授权登陆成功后跳转到微信服务号关注页面: phpqrcode类库下载在此不提供各位可以百度搜索下载 magento微信扫码网站自动登录的例子 查看授权后接口调用(UnionID),不难发现填写回调地址,用户确认登陆pc端即可跳转 获取UnionID方法 更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP微信开发技巧汇总》、《PHP编码与转码操作技巧汇总》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》 希望本文所述对大家PHP程序设计有所帮助。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);}}
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 = '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 ''
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CNpublic 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 '
错误:
'.$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 '
';//获取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号第一次登录}您可能感兴趣的文章:
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号