php实现微信扫码自动登陆与注册功能


Posted in PHP onSeptember 22, 2016

本文实例讲述了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你传入什么微信那边返回什么

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

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

此时生成了二维码 state是标识,phpqrcode可以在文章末尾下载,这样我们设置了回调地址http://www.xxx.net/login/wcallback

就可以在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 = '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类库下载在此不提供各位可以百度搜索下载

magento微信扫码网站自动登录的例子
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN

查看授权后接口调用(UnionID),不难发现填写回调地址,用户确认登陆pc端即可跳转

获取UnionID方法

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>';
// print_r($user_info);
// echo '</pre>';
//获取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 相关文章推荐
PHP图片验证码制作实现分享(全)
May 10 PHP
一个不易被发现的PHP后门代码解析
Jul 05 PHP
PHP会话处理的10个函数
Aug 11 PHP
php常用图片处理类
Mar 16 PHP
PHP+MySQL存储数据常见中文乱码问题小结
Jun 13 PHP
php解析xml 的四种简单方法(附实例)
Jul 11 PHP
php操作access数据库的方法详解
Feb 22 PHP
PHP实现上传多图即时显示与即时删除的方法
May 09 PHP
基于PHP实现栈数据结构和括号匹配算法示例
Aug 10 PHP
PHP基于mcript扩展实现对称加密功能示例
Feb 21 PHP
Yii2框架加载css和js文件的方法分析
May 25 PHP
php使用event扩展的io复用测试的示例
Oct 20 PHP
php版微信公众号自定义分享内容实现方法
Sep 22 #PHP
php版微信公众平台接口开发之智能回复开发教程
Sep 22 #PHP
PHP判断用户是否已经登录(跳转到不同页面或者执行不同动作)
Sep 22 #PHP
PHP查看SSL证书信息的方法
Sep 22 #PHP
php版微信自动获取收货地址api用法示例
Sep 22 #PHP
php版微信公众平台回复中文出现乱码问题的解决方法
Sep 22 #PHP
php中foreach结合curl实现多线程的方法分析
Sep 22 #PHP
You might like
php htmlspecialchars()与shtmlspecialchars()函数的深入分析
2013/06/05 PHP
如何用C语言编写PHP扩展的详解
2013/06/13 PHP
PHP+sqlite数据库操作示例(创建/打开/插入/检索)
2016/05/26 PHP
JavaScript传递变量: 值传递?引用传递?
2011/02/22 Javascript
让textarea自动调整大小的js代码
2011/04/12 Javascript
jquery中常用的函数和属性详细解析
2014/03/07 Javascript
js的toLowerCase方法用法实例
2015/01/27 Javascript
Bootstrap每天必学之折叠
2016/04/12 Javascript
javaScript 事件绑定、事件冒泡、事件捕获和事件执行顺序整理总结
2016/10/10 Javascript
JS调用打印机功能简单示例
2016/11/28 Javascript
微信公众平台开发教程(六)获取个性二维码的实例
2016/12/02 Javascript
JS日程管理插件FullCalendar简单实例
2017/02/07 Javascript
基于canvas的二维码邀请函生成插件
2017/02/14 Javascript
JavaScript实现审核流程状态的动态显示进度条
2017/03/15 Javascript
jQuery实现文章图片弹出放大效果
2017/04/06 jQuery
vue-prop父组件向子组件进行传值的方法
2018/03/01 Javascript
ES6 中可以提升幸福度的小功能
2018/08/06 Javascript
node.js爬取中关村的在线电瓶车信息
2018/11/13 Javascript
vue实现购物车小案例
2019/09/27 Javascript
jquery制作的移动端购物车效果完整示例
2020/02/24 jQuery
javascript设计模式 ? 组合模式原理与应用实例分析
2020/04/14 Javascript
Python字符串的encode与decode研究心得乱码问题解决方法
2009/03/23 Python
Python3.6.0+opencv3.3.0人脸检测示例
2018/05/25 Python
win8下python3.4安装和环境配置图文教程
2018/07/31 Python
解决python ogr shp字段写入中文乱码的问题
2018/12/31 Python
基于python的docx模块处理word和WPS的docx格式文件方式
2020/02/13 Python
python字符串常用方法及文件简单读写的操作方法
2020/03/04 Python
python如何将两张图片生成为全景图片
2020/03/05 Python
jenkins+python自动化测试持续集成教程
2020/05/12 Python
用sleep间隔进行python反爬虫的实例讲解
2020/11/30 Python
Sunglasses Shop瑞典:欧洲领先的太阳镜网上商店
2018/04/22 全球购物
暑期实习鉴定
2013/12/16 职场文书
运动会开幕式邀请函
2014/02/03 职场文书
孙振耀退休感言
2015/08/01 职场文书
研究生学习计划书应该怎么写?
2019/09/10 职场文书
golang 实现时间戳和时间的转化
2021/05/07 Golang