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。