OAuth容許用戶提供一個令牌,而不是用戶名和密碼來訪問它們存放在特定服務器上的數據,每個令牌受權一個特定的網站在特定時段內訪問特定的資源。javascript
受權過程以下:php
一、引導用戶進入受權頁面贊成受權,獲取code css
二、經過code換取網頁受權access_token(與基礎支持中的access_token不一樣) html
三、若是須要,開發者能夠刷新網頁受權access_token,避免過時 java
四、經過網頁受權access_token和openid獲取用戶基本信息(支持UnionID機制) json
<?php if (isset($_GET['code'])){ echo "code:".$_GET['code']."<br>"; echo "state:".$_GET["state"]; }else { echo "no code"; }
<?php @header('Content-type: text/html;charset=UTF-8'); //設置時區 date_default_timezone_set("Asia/Shanghai"); //定義TOKEN常量,這裏的"weixin"就是在公衆號裏配置的TOKEN require_once("Utils.php"); //打印請求的URL查詢字符串到query.xml Utils::traceHttp(); $wechatObj = new wechatCallBackapiTest(); $wechatObj->responseMsg(); class wechatCallBackapiTest { public function responseMsg() { //獲取post過來的數據,它一個XML格式的數據 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //將數據打印到log.xml Utils::logger($postStr); if (!empty($postStr)) { //將XML數據解析爲一個對象 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); //消息類型分離 switch($RX_TYPE) { case "text": $result = $this->receiveText($postObj); break; default: $result = ""; break; } Utils::logger($result, '公衆號'); echo $result; }else { echo ""; exit; } } private function receiveText($object) { $appid = "wx07fff9c79a410b69"; $redirect_uri = urlencode("http://weiweiyi.duapp.com/oauth/oauth2.php"); $keyword = trim($object->Content); if(strstr($keyword, "base")){ $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=". "$redirect_uri&response_type=code&scope=snsapi_base&state=123#wechat_redirect"; $content = "用戶受權snsapi_base實現:<a href='$url'>單擊這裏體驗OAuth受權</a>"; }else if (strstr($keyword, "userinfo")){ $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=". "$redirect_uri&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; $content = "用戶受權snsapi_userInfo實現:<a href='$url'>單擊這裏體驗OAuth受權</a>"; }else{ $content = ""; } $result = $this->transmitText($object, $content); return $result; } /** * 回覆文本消息 */ private function transmitText($object, $content) { $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime><![CDATA[%s]]></CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } }
<?php @header('Content-type: text/plain;charset=UTF-8'); require_once("../Utils.php"); $appid = "wx07fff9c79a410b69"; $appsecret = "092c0c0c5bd62f66b76ad241612915fb"; $code = "0016Q5kn0zdDFp1E3rjn0VUjkn06Q5km"; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?" ."appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code"; $result = Utils::https_request($url); echo $result;若是是base,受權流程到此結束,返回:
{ "access_token": "b3e1GZdT1E-sjKzeKRCr9XUE6IQglkBBxrFXdsmZ8DVW4O5t16EXbIxCoob6pGXwA5Z9JubOZnIytGcM5xC20g", "expires_in": 7200, "refresh_token": "yJkiFmmRVq5Kst6PiZpwGPvJh0bcegccx-KFIZEIwYKRmdiLC5dG8-iMRkjl1Stf8cSrHjDauzZtEGNHlnGckA", "openid": "o4WmZ0h-4huBUVQUczx2ezaxIL9c", "scope": "snsapi_base" }若是是userinfo,返回:
{ "access_token": "iUIP_RnPmjVICZtmq6fFRcslRD1yJax3IkeT_fXKFlDv5W_9y5JS4Z4QgC1W33Qi2BbQ5pWLWt-6LYT7u1Egvg", "expires_in": 7200, "refresh_token": "rKwY7NF0BqfSpLVwmVO-htyvlrFWQVRmCdimoaLG2JiHz8wEJZ2H7fcQ5wtJylixBt-dCENgasbaSs8_7M-Kmw", "openid": "o4WmZ0h-4huBUVQUczx2ezaxIL9c", "scope": "snsapi_userinfo" }
<?php @header('Content-type: text/plain;charset=UTF-8'); require_once("../Utils.php"); $appid = "wx07fff9c79a410b69"; $appsecret = "092c0c0c5bd62f66b76ad241612915fb"; $refresh_token = "rKwY7NF0BqfSpLVwmVO-htyvlrFWQVRmCdimoaLG2JiHz8wEJZ2H7fcQ5wtJylixBt-dCENgasbaSs8_7M-Kmw"; $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?" ."appid=$appid&grant_type=refresh_token&refresh_token=$refresh_token"; $result = Utils::https_request($url); echo $result;返回:
{ "openid": "o4WmZ0h-4huBUVQUczx2ezaxIL9c", "access_token": "iUIP_RnPmjVICZtmq6fFRcslRD1yJax3IkeT_fXKFlDv5W_9y5JS4Z4QgC1W33Qi2BbQ5pWLWt-6LYT7u1Egvg", "expires_in": 7200, "refresh_token": "rKwY7NF0BqfSpLVwmVO-htyvlrFWQVRmCdimoaLG2JiHz8wEJZ2H7fcQ5wtJylixBt-dCENgasbaSs8_7M-Kmw", "scope": "snsapi_base,snsapi_userinfo," }
<?php @header('Content-type: text/plain;charset=UTF-8'); require_once("../Utils.php"); $access_token = "iUIP_RnPmjVICZtmq6fFRcslRD1yJax3IkeT_fXKFlDv5W_9y5JS4Z4QgC1W33Qi2BbQ5pWLWt-6LYT7u1Egvg"; $openid = "o4WmZ0h-4huBUVQUczx2ezaxIL9c"; $url = "https://api.weixin.qq.com/sns/userinfo?" ."access_token=$access_token&openid=$openid&lang=zh_CN "; $result = Utils::https_request($url); echo $result;返回:
{ "openid": "o4WmZ0h-4huBUVQUczx2ezaxIL9c", "nickname": "Promise", "sex": 1, "language": "zh_CN", "city": "", "province": "", "country": "", "headimgurl": "http://wx.qlogo.cn/mmopen/vi_32/um6ptBDhpau47ctyJHMakZgyHJsYHzjMfouyWqP6DNxNEPLf2uk6V6TBNnsbanrUcABJiaEa74W8VB7JRk9k0kg/0", "privilege": [] }受權過程到此結束。
<?php @header('Content-type: text/plain;charset=UTF-8'); require_once("../Utils.php"); $code = $_GET["code"]; $userinfo = getUserInfo($code); echo $userinfo; function getUserInfo($code) { $appid = "wx07fff9c79a410b69"; $appsecret = "092c0c0c5bd62f66b76ad241612915fb"; //根據code得到access_token $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?" ."appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code"; $access_token_json = Utils::https_request($access_token_url); $access_token_array = json_decode($access_token_json, true); //access_token $access_token = $access_token_array["access_token"]; //openid $openid = $access_token_array["openid"]; //根據access_token和openid得到用戶信息 $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?" ."access_token=$access_token&openid=$openid&lang=zh_CN "; $userinfo_json = Utils::https_request($userinfo_url); return $userinfo_json; }注意:獲取的code只能使用一次,超過一次會報40163的錯誤,這時會獲取不到access_token。