PHP實現微信掃碼自動登陸與注冊,參考實例


 

微信開發已經是現在phper必須要掌握的一項基本的技術了,其實做過微信開發的都知道微信接口非常的強大做起來也非常的簡單,這里我們一起來看一個微信自動登陸注冊的例子.

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

一、首先把微信鏈接帶個標識生成二維碼

比如鏈接為

'.$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 }
 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 $errorCorrectionLevel = "L";
12 $matrixPointSize = "5";
13 QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
14 }
15 }

 

此時生成了二維碼 state是標識,phpqrcode可以在文章末尾下載,這樣我們設置了回調地址

就可以在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 if (emptyempty($code)) $this->showMessage('授權失敗');
 9 try{
10 $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'
11 $token = json_decode($this->https_request($token_url));
12 }catch(Exception $e)
13 {
14 print_r($e);
15 }
16 if (isset($token->errcode)) {
17 echo '錯誤:'.$token->errcode;
18 echo '錯誤信息:'.$token->errmsg;
19 exit;
20 }
21 $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
22 //轉成對象
23 $access_token = json_decode($this->https_request($access_token_url));
24 if (isset($access_token->errcode)) {
25 echo '錯誤:'.$access_token->errcode;
26 echo '錯誤信息:'.$access_token->errmsg;
27 exit;
28 }
29 $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN'
30 //轉成對象
31 $user_info = json_decode($this->https_request($user_info_url));
32 if (isset($user_info->errcode)) {
33 echo '錯誤:'.$user_info->errcode;
34 echo '錯誤信息:'.$user_info->errmsg;
35 exit;
36 }
37 //打印用戶信息
38 // echo ''
39 // print_r($user_info);
40 // echo ''

 

phpqrcode類庫下載在此不提供各位可以百度搜索下載

magento微信掃碼網站自動登錄的例子

查看授權后接口調用(UnionID),不難發現填寫回調地址,用戶確認登陸pc端即可跳轉

 

獲取UnionID方法

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM