当前位置:首页 > PHP教程 > php高级应用 > 列表

微信扫码网站自动登录的实现方法

发布:smiling 来源: PHP粉丝网  添加日期:2015-12-24 13:48:25 浏览: 评论:0 

微信扫码网站自动登录的原是还是比较简单的,只要各位知道相互的原理就可以实现了,下面我们来看两个例子,我相信各位看了这两个例子肯定知道怎么来做了.

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. public function wcallbackAction(){ 
  2.  
  3. $code = $_GET['code']; 
  4. $state = $_GET['state']; 
  5. $setting = include CONFIG_PATH . 'setting.php'
  6. $appid=$setting['weixin']['appid']; 
  7. $appsecret=$setting['weixin']['appsecret']; 
  8.  
  9. if (emptyempty($code)) $this->showMessage('授权失败'); 
  10. try{ 
  11.  
  12. $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'
  13.  
  14. $token = json_decode($this->https_request($token_url)); 
  15.  
  16. }catch(Exception $e
  17. print_r($e); 
  18.  
  19. if (isset($token->errcode)) { 
  20. echo '<h1>错误:</h1>'.$token->errcode; 
  21. echo '<br/><h2>错误信息:</h2>'.$token->errmsg; 
  22. exit
  23.  
  24. $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token; 
  25. //转成对象 
  26. $access_token = json_decode($this->https_request($access_token_url)); 
  27. if (isset($access_token->errcode)) { 
  28. echo '<h1>错误:</h1>'.$access_token->errcode; 
  29. echo '<br/><h2>错误信息:</h2>'.$access_token->errmsg; 
  30. exit
  31. $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN'
  32. //转成对象 
  33. $user_info = json_decode($this->https_request($user_info_url)); 
  34. if (isset($user_info->errcode)) { 
  35. echo '<h1>错误:</h1>'.$user_info->errcode; 
  36. echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg; 
  37. exit
  38. //打印用户信息 
  39. // echo '<pre>'; 
  40. // print_r($user_info); 
  41. // echo '</pre>'; 
  42.  
  43. //获取unionid 
  44.  
  45. $uid=$user_info->unionid; 
  46.  
  47.  
  48. //用户操作处理 分为再次登录和第一次登陆 
  49. //phpfensi.com 
  50. $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='"
  51. User::$arrUtype['weixin_num_t']."' and t2.openid='$user_info->unionid'"
  52. $h_user_id = Core_Db::getOne($sql); 
  53. if(!emptyempty($h_user_id)){//该weixin号再次登录 
  54.  
  55. }{//该weixin号第一次登录 
  56.  

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. public function creatqrAction(){ 
  2. if($_GET['app']){ 
  3. $wtoken=$_COOKIE['wtoken']; 
  4. $postdata=$_SESSION['w_state']; 
  5. if($wtoken){ 
  6. $postdata=$wtoken
  7. <span style="color: #3366ff;">include CONFIG_PATH . 'phpqrcode/'.'phpqrcode.php';</span> 
  8. $sh=$this-&gt;shar1(); 
  9. $value="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx138697ef383a9167&amp;redirect_uri=http://www.xxx.net/login/wcallback&amp;response_type=code&amp;scope=snsapi_userinfo&amp;state=".$postdata."&amp;connect_redirect=1#wechat_redirect"
  10. $errorCorrectionLevel = "L"
  11. $matrixPointSize = "5"
  12. QRcode::png($value, false, $errorCorrectionLevel$matrixPointSize); 

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

就可以在wcallback方法里面处理数据 插入用户 生成session,跳转登陆,pc端可以设置几秒钟ajax请求服务器,一旦获取到了

state,即实现调整,微信浏览器里处理完后可以关闭窗口,微信js可实现

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

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

  1. header("Location: weixin://profile/gh_a5e1959f9a4e"); 
  2. wcallback方法做处理登陆 
  3.  
  4. $code = $_GET['code']; 
  5. $state = $_GET['state']; 
  6. $setting = include CONFIG_PATH . 'setting.php'
  7. $appid=$setting['weixin']['appid']; 
  8. $appsecret=$setting['weixin']['appsecret']; 
  9. if (emptyempty($code)) $this->showMessage('授权失败'); 
  10. try{ 
  11. $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'
  12. $token = json_decode($this->https_request($token_url)); 
  13. }catch(Exception $e
  14. print_r($e); 
  15. if (isset($token->errcode)) { 
  16. echo '<h1>错误:</h1>'.$token->errcode; 
  17. echo '<br/><h2>错误信息:</h2>'.$token->errmsg; 
  18. exit
  19. $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token; 
  20. //转成对象 
  21. $access_token = json_decode($this->https_request($access_token_url)); 
  22. if (isset($access_token->errcode)) { 
  23. echo '<h1>错误:</h1>'.$access_token->errcode; 
  24. echo '<br/><h2>错误信息:</h2>'.$access_token->errmsg; 
  25. exit
  26. $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN'
  27. //转成对象 
  28. $user_info = json_decode($this->https_request($user_info_url)); 
  29. if (isset($user_info->errcode)) { 
  30. echo '<h1>错误:</h1>'.$user_info->errcode; 
  31. echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg; 
  32. exit
  33. //打印用户信息 
  34. // echo '<pre>'; 
  35. // print_r($user_info); 
  36. // echo '</pre>';

Tags: php微信扫码 php自动登录

分享到: