php实现微信扫码自动登陆与注册功能
本文实例讲述了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你传入什么微信那边返回什么
可以作为服务器与微信段的一个标识:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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可实现:
|
1
2
|
document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {WeixinJSBridge.call('closeWindow');}, false); |
也可以授权登陆成功后跳转到微信服务号关注页面:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
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实现微信扫码自动登陆与注册,参考实例
微信开发已经是现在phper必须要掌握的一项基本的技术了,其实做过微信开发的都知道微信接口非常的强大做起来也非常的简单,这里我们一起来看一个微信自动登陆注册的例子. php 微信扫码 pc端自动登陆注 ...
- asp源码微信扫码授权登陆电脑版
网站接入微信扫码登录并获取用户基本信息(完美绕过微信开放平台)电脑版网站实现微信扫码登录,注册会员还要设密码太麻烦,会员也记不住密码,采用微信扫码登录网站更方便,会员无需设密码,用他的微信做为系统登录 ...
- ASP.NET Core Web 支付功能接入 微信-扫码支付篇
这篇文章将介绍ASP.NET Core中使用 开源项目 Payment,实现接入微信-扫码支付及异步通知功能. 开发环境:Win 10 x64.VS2017 15.6.4..NET Core SDK ...
- 【转载】ASP.NET Core Web 支付功能接入 微信-扫码支付篇
转自:http://www.cnblogs.com/essenroc/p/8630730.html 这篇文章将介绍ASP.NET Core中使用 开源项目 Payment,实现接入微信-扫码支付及异步 ...
- ASP.NET Core Web 支付功能接入 微信-扫码支付篇(转)
原文 https://www.cnblogs.com/essenroc/p/8630730.html // 随着版本更迭,新版本可能无法完全适用,请参考仓库内的示例. 这篇文章将介绍ASP.NET C ...
- Tapdata Cloud 2.1.2 来啦:大波细节已就绪!字段类型可批量修改、支持微信扫码登录、新增支持 Vika 为目标
Tapdata Cloud cloud.tapdata.net 让数据实时可用 Tapdata Cloud 是国内首家异构数据库实时同步云平台,目前支持 Oracle.MySQL.PG.SQL Ser ...
- 分享一下,PHP实现第四方QQ微信扫码登陆,不接入qq互联以及微信开发者平台就可以实现用户对接鹅厂,phpQQ微信扫码登陆
自己抓的QQ包以及整合了网上一些已经封装好了的代码具体如下:QQ: <?php class QQ extends Curl_Api { //获取登录验证码 public function QRc ...
- 【微信开发】PC端 微信扫码支付成功之后自动跳转
场景: PC端 微信扫码支付 结果: 支付成功 自动跳转 实现思路: 支付二维码页面,写ajax请求支付状态,请求到结果,无论成功还是失败,都跳转到相应的结果页面 具体实现方法: html部分: ...
- 微信扫码登陆(JAVA)
在web端用到weChat扫码登录,在手机扫码登陆成功后,跳转到相应的界面. 1.第一步请求code 调用接口:https://open.weixin.qq.com/connect/qrconnect ...
随机推荐
- Qt for Android 启动短暂的黑屏或白屏问题如何解决?
解决方法一: 使用透明主题 点击项目 -> 在 构建设置 里面找到 Build Android APK 栏目,点击 create templates 创建一个 AndroidManifest.x ...
- 基于Cpython的 GIL(Global Interpreter Lock)
一 介绍 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native t ...
- 常用模块一(os模块、序列化模块(json和pickle))
一.os模块 os模块是与操作系统交互的一个接口. import os # 和文件和文件夹的操作有关 os.makedirs('dirname1/dirname2') # 可生成多层递归目录 os.r ...
- Java基础—反射(转载)
转载自: JAVA反射与注解 JAVA反射 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义. 反射机制是什么 反射 ...
- STL之map、set灵活使用
1.LA 5908/UVA1517 Tracking RFIDs 题意:给出s个传感器的位置,以及其感应范围.如果某个方向上有墙,则该方向上感应距离减1.现在有w个墙,给出p个物品的位置,问其能被几个 ...
- SOA和微服务之间的区别
近几年,我们有很多文章对SOA和微服务之间的不同点和相似点进行了分析.有些人认为SOA有很多地方是值得微服务学习的,而有些人则认为区别对待微服务和SOA会更好.而Neal Ford认为,将单体迁移到面 ...
- OS X 与传统Unix的一点区别
在传统的Unix系统或者Linux系统中,你是很难在根目录下找到大写开头的文件夹的, 但是看一下OS X: ls / Applications Users etc private var Develo ...
- octotree神器 For Github and GitLab 火狐插件
Code tree for GitHub and GitLabExtension to show code tree for GitHub and GitLab. Useful for develop ...
- Android DDMS应用
具体可见http://developer.android.com/tools/debugging/ddms.html. DDMS为IDE和emultor.真正的android设备架起来了一座桥梁.开发 ...
- RHCE学习笔记 管理1 (第三~五章)
第三章 红帽企业linux 获取帮助 (略) man .pinfo. 第四章 编辑文件 1.输出重定向到文件和程序 >file 定向文件(覆盖) >>file 定向文件(附 ...