国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php 微信掃碼自動登陸注冊例子

2024-05-04 21:49: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.  
  3. if($_GET['app']){ 
  4. $wtoken=$_COOKIE['wtoken']; 
  5. $postdata=$_SESSION['w_state']; 
  6. if($wtoken){ 
  7. $postdata=$wtoken
  8. include CONFIG_PATH . 'phpqrcode/'.'phpqrcode.php' 
  9. $sh=$this->shar1(); 
  10. $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"
  11.  
  12. $errorCorrectionLevel = "L"
  13.  
  14. $matrixPointSize = "5"
  15.  
  16. QRcode::png($value, false, $errorCorrectionLevel$matrixPointSize); 
  17.  

此時生成了二維碼 state是標識,phpqrcode可以在文章末尾下載,這樣我們設置了回調地址http://www.xxx.net/login/wcallback

就可以在wcallback方法里面處理數據 插入用戶 生成session,跳轉登陸,pc端可以設置幾秒鐘ajax請求服務器,一旦獲取到了state,即實現調整,微信瀏覽器里處理完后可以關閉窗口,微信js可實現:

document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {

WeixinJSBridge.call('closeWindow');}, false);

也可以授權登陸成功后跳轉到微信服務號關注頁面:

  1. header("Location: weixin://profile/gh_a5e1959f9a4e"); 
  2. wcallback方法做處理登陸 
  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 ' 
  21. 錯誤: 
  22. '.$token->errcode; 
  23. echo ' 
  24. 錯誤信息: 
  25. '.$token->errmsg; 
  26. exit
  27.  
  28. $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token; 
  29. //轉成對象 
  30. $access_token = json_decode($this->https_request($access_token_url)); 
  31. if (isset($access_token->errcode)) { 
  32. echo ' 
  33. 錯誤: 
  34. '.$access_token->errcode; 
  35. echo ' 
  36. 錯誤信息: 
  37. '.$access_token->errmsg; 
  38. exit
  39. $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN' 
  40. //轉成對象 
  41. $user_info = json_decode($this->https_request($user_info_url)); 
  42. if (isset($user_info->errcode)) { 
  43. echo ' 
  44. 錯誤: 
  45. '.$user_info->errcode; 
  46. echo ' 
  47. 錯誤信息: 
  48. '.$user_info->errmsg; 
  49. exit
  50. //打印用戶信息 
  51. // echo ' 
  52. // print_r($user_info); 
  53. // echo ' 
  54. phpqrcode類庫下載在此不提供各位可以百度搜索下載 
  55. magento微信掃碼網站自動登錄的例子 
  56. https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN 
  57. 查看授權后接口調用(UnionID),不難發現填寫回調地址,用戶確認登陸pc端即可跳轉 
  58. 獲取UnionID方法 
  59.  
  60. public function wcallbackAction(){ 
  61.  
  62. $code = $_GET['code']; 
  63. $state = $_GET['state']; 
  64. $setting = include CONFIG_PATH . 'setting.php'
  65. $appid=$setting['weixin']['appid']; 
  66. $appsecret=$setting['weixin']['appsecret']; 
  67.  
  68. if (emptyempty($code)) $this->showMessage('授權失敗'); 
  69. try{ 
  70.  
  71. $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'
  72.  
  73. $token = json_decode($this->https_request($token_url)); 
  74.  
  75. }catch(Exception $e
  76. print_r($e); 
  77.  
  78. if (isset($token->errcode)) { 
  79. echo '<h1>錯誤:</h1>'.$token->errcode; 
  80. echo '<br/><h2>錯誤信息:</h2>'.$token->errmsg; 
  81. exit
  82.  
  83. $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token; 
  84. //轉成對象 
  85. $access_token = json_decode($this->https_request($access_token_url)); 
  86. if (isset($access_token->errcode)) { 
  87. echo '<h1>錯誤:</h1>'.$access_token->errcode; 
  88. echo '<br/><h2>錯誤信息:</h2>'.$access_token->errmsg; 
  89. exit
  90. $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN'
  91. //轉成對象 
  92. $user_info = json_decode($this->https_request($user_info_url)); 
  93. if (isset($user_info->errcode)) { 
  94. echo '<h1>錯誤:</h1>'.$user_info->errcode; 
  95. echo '<br/><h2>錯誤信息:</h2>'.$user_info->errmsg; 
  96. exit
  97. //打印用戶信息 
  98. // echo '<pre>'; 
  99. // print_r($user_info); 
  100. // echo '</pre>'; 
  101.  
  102. //獲取unionid 
  103.  
  104. $uid=$user_info->unionid; 
  105.  
  106. //Vevb.com 
  107. //用戶操作處理 分為再次登錄和第一次登陸 
  108.  
  109. $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='"
  110. User::$arrUtype['weixin_num_t']."' and t2.openid='$user_info->unionid'"
  111. $h_user_id = Core_Db::getOne($sql); 
  112. if(!emptyempty($h_user_id)){//該weixin號再次登錄 
  113.  
  114. }{//該weixin號第一次登錄 
  115.  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 库车县| 长阳| 阿图什市| 汕头市| 岳池县| 乌兰察布市| 洞头县| 南雄市| 普兰店市| 永年县| 酒泉市| 黔南| 娱乐| 甘南县| 蒙自县| 岱山县| 勐海县| 诸暨市| 永宁县| 溧阳市| 会泽县| 高淳县| 隆子县| 潞城市| 天全县| 城固县| 乌审旗| 北流市| 新和县| 京山县| 柳林县| 左贡县| 广昌县| 三门县| 安顺市| 子长县| 吉林市| 阜新市| 招远市| 湘乡市| 花垣县|