微信第三方登錄授權


首先第三方應用要跟微信帳號合作,然后按照下面的方法操作:

第一步:注冊應用。

可以通過衛微信的開放平台去注冊一個應用。之后你會得到一個App Key和一個App Secret。擁有它們,你才可以申請權限。

假設你的App Key是“1234567890”,App Secret是“abcdefghijklmnopqrstuvwxyz"

第二步:寫代碼。

將獲取到的OAuth的php版本的SDK加入你的project中。將你申請到的Key和Secret做為兩個變量定義並賦值。

對於OAuth來說,很多細節不需要我們去關注的,只要知道幾個重要的步驟即可:

 1. 第三方發起微信授權登錄請求,微信用戶允許授權第三方應用后,微信會拉起應用或重定向到第三方網站,並且帶上授權臨時票據code參數;

 2. 通過code參數加上AppID和AppSecret等,通過API換取access_token;

 3. 通過access_token進行接口調用,獲取用戶基本數據資源或幫助用戶實現基本操作。獲取access_token時

  4. 獲得未授權的access_token這個獲得未授權的 access_token就相當於放行條,也就是微信允許你開始獲取用戶的權限。

  5. 根據這個access_token的內容,獲得一個url地址,這個地址頁面就是想登錄你應用的用戶輸入用戶名和密碼的地方。注意的是,這個url是屬於微信為你的這個應用創建的回調地址。

  6. 用戶在上述登錄界面輸入自己的用戶名和密碼,成功登錄之后,你可以獲得已授權的 Access KEY。這個Access Key就包含了用戶多登錄信息(包括昵稱、用戶openID等等,這里的昵稱是指用戶顯示在微信上的名字,而不是用戶的登錄名)。

代碼:

fn_system.php 判斷微信登錄是否已經授權

<?php if(empty($_SESSION['user'])){ header("Location:fn_wx_login.php"); }else{ print_r($_SESSION['user']); } ?>  

wx_sample.php 微信授權文件

<?php /** * wechat php test */

//define your token /* token 一定要和微信開發平台上填寫的保持一致*/
define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->valid(); class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option
        if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data
        if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!empty( $keyword )) { $msgType = "text"; $contentStr = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } private function checkSignature() { // you must define TOKEN by yourself
        if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule
        sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>

fn_callback.php 微信回調文件,通過token獲取用戶的基本資料

<?php /*設置appid和secret以及回調地址*/
$appid = "123456789"; $secret = "abcdefghijklmnopqrstuvwxyz"; $code = $_GET["code"]; $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code'; $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$get_token_url); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); $res = curl_exec($ch); curl_close($ch); $json_obj = json_decode($res,true); //根據openid和access_token查詢用戶信息 
$access_token = $json_obj['access_token']; $openid = $json_obj['openid']; $get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN'; $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$get_user_info_url); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); $res = curl_exec($ch); curl_close($ch); //解析json 
$user_obj = json_decode($res,true); if($user_obj){ // 處理第三方登錄信息
    $sql = "select * from `ub01` where ub01004 = '".$user_obj['openid']."'"; $chek = _selectone($sql) ? _selectone($sql) : array(); if(!empty($chek)){ $_SESSION['uid']=$res['ua01001']; header("Location:?"); }else{ $arr['ua01998'] = time(); $arr['ua01005'] = 2; $arr['guid'] = UUID(); $id = _inserttable('ua01',$arr,true); if($id){ $brr['ua01001'] = $id; $brr['ub01003'] = 2; $brr['ub01004'] = $user_obj['openid']; $arr['ub01998'] = time(); $brr['guid'] = UUID(); $sid = _inserttable('ua01',$arr,true); if($sid){ $sql = "select * from `ub01` where ub01001 = '$sid'"; $cheks = _selectone($sql) ? _selectone($sql) : array(); if(!empty($cheks)){ $_SESSION['uid']=$res['ua01001']; header("Location:?"); } } } } // $_SESSION['user'] = $user_obj; 
} // print_r($user_obj); 
  
?>  

 


免責聲明!

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



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