php實現微信掃碼自動登陸與註冊功能的方法

來源:互聯網
上載者:User
這篇文章主要介紹了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你傳入什麼那邊返回什麼

可以作為伺服器與段的一個標識:

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號第一次登入}

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.