(亲测有效) 微信公众平台开发(71)OAuth2.0网页授权
https://www.cnblogs.com/txw1958/p/weixin71-oauth20.html
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd1fc65357aca6efa&redirect_uri=http%3A%2F%2F1.dq095.applinzi.com%2Fdiner%2Flogin.php&response_type=code&scope=snsapi_base&state=235#wechat_redirect
https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd1fc65357aca6efa&secret=55a5a3a73b3f1603240a1c27cb0b270f&code=001H4iD42zU1kM0CG9C42DCtD42H4iDw&grant_type=authorization_code{"access_token":"14_fJ52tyRrsQG-PDKIP4JC_XI-MPsd7SK0swntkoBZ6nMPsPSvHrrnpSixoSAX-eODlyQVmL10gYdD6cdO0J5JeVOTncoXrNXWxXSr8GAEDLc","expires_in":7200,"refresh_token":"14_zHB5y6q9bfvL_q_4l19NrXclyKq1moHmwj3JI5NIUkKijVCCvm13bWretQj2Cqcs-VaeqvkCI8bLOIlh9wxKWKuedislliL2fIlSpNyqfOw","openid":"oXpyI03cX4bpCDW5aaqBKWYrRm_o","scope":"snsapi_userinfo"}
{"access_token": "14_fJ52tyRrsQG-PDKIP4JC_XI-MPsd7SK0swntkoBZ6nMPsPSvHrrnpSixoSAX-eODlyQVmL10gYdD6cdO0J5JeVOTncoXrNXWxXSr8GAEDLc","expires_in": 7200,"refresh_token": "14_zHB5y6q9bfvL_q_4l19NrXclyKq1moHmwj3JI5NIUkKijVCCvm13bWretQj2Cqcs-VaeqvkCI8bLOIlh9wxKWKuedislliL2fIlSpNyqfOw","openid": "oXpyI03cX4bpCDW5aaqBKWYrRm_o","scope": "snsapi_userinfo"
}https://api.weixin.qq.com/sns/userinfo?access_token=14_fJ52tyRrsQG-PDKIP4JC_XI-MPsd7SK0swntkoBZ6nMPsPSvHrrnpSixoSAX-eODlyQVmL10gYdD6cdO0J5JeVOTncoXrNXWxXSr8GAEDLc&openid=oXpyI03cX4bpCDW5aaqBKWYrRm_o
{"openid": "oXpyI03cX4bpCDW5aaqBKWYrRm_o","nickname": "?德强?","sex": 1,"language": "zh_CN","city": "Jiujiang","province": "Jiangxi","country": "CN","headimgurl": "http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Fq9P1FUrJILEqCDbCaQH2EFvh7JiagpcEicq279uNv9SevpiabCoiaaCwjLeotGMOAr8WqU2GOQtJGibsbwfFqPMJ3w\/132","privilege": []
}
case "授权":$content = 'OAuth2.0网页授权演示 <a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd1fc65357aca6efa&redirect_uri=http://1.dq095.applinzi.com/oauth2.php&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect">OAuth2</a>';
示例代码如下:
index.php
private function receiveText($object){$keyword = trim($object->Content);switch($keyword){case "授权":$content = 'OAuth2.0网页授权演示 <a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd1fc65357aca6efa&redirect_uri=http://1.dq095.applinzi.com/oauth2.php&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect">OAuth2</a>';break;default:include("xiaoi.php");$content = getXiaoiInfo($object->FromUserName, $keyword);}$result = $this->transmitText($object, $content);return $result;}
OAuth2.php
<?phprequire "diner/lib/weixin.class.php";
if (!isset($_GET['code'])){echo "NO CODE";
}$token = weixin::getAuthToken($_GET['code']);
$userinfo = weixin::getUserInfoByOAuth($token['access_token'],$token['openid']);switch($userinfo[sex]){case 0:$userinfo[sex] ="未知";break;case 1:$userinfo[sex] ="男";break;case 2:$userinfo[sex] ="女";break;}
?>
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title></title>
</head>
<body><div class="content"><p><label>OpenId</label></p><p><input type="text" readonly="readonly" value="<?php echo $userinfo['openid']?>"></p><p><label>昵称</label></p><p><input type="text" readonly="readonly" value="<?php echo $userinfo['nickname']?>"></p><p><label>性别</label></p><p><input type="text" readonly="readonly" value="<?php echo $userinfo['sex']?>"></p><p><label>省份</label></p><p><input type="text" readonly="readonly" value="<?php echo $userinfo['province']?>"></p><p><label>城市</label></p><p><input type="text" readonly="readonly" value="<?php echo $userinfo['city']?>"></p><p><label>头像</label></p><img src="<?php echo $userinfo['headimgurl']?>"><p><input type="text" readonly="readonly" value="<?php echo $userinfo['headimgurl']?>"></p></div></body>
</html>
weixin.class.php
/*** 获取用户授权code url* @param type $scope 授权作用域:snsapi_base or snsapi_userinfo* @param type $state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值* @return type*/public static function createCodeUrl($scope,$state){$open_url = 'https://open.weixin.qq.com';$redirect_url = urlencode('http://1.dq095.applinzi.com/oauth2.php');$url = $open_url.'/connect/oauth2/authorize?appid='.APPID.'&redirect_uri='.$redirect_url.'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';return $url;}/*** 获取用户授权access_token* @param type $code 授权时获得code值* @return type*/public static function getAuthToken($code){$url = self::API_URL.'/sns/oauth2/access_token?appid='.APPID.'&secret='.APPSECRET.'&code='.$code.'&grant_type=authorization_code';$content = curl_get( $url );$ret = json_decode($content, true );return self::getResult( $ret ) ? $ret : null;}/*** 刷新用户授权access_token* @param type $refresh_token 用户刷新access_token* @return type*/public static function refershAuthToken($refresh_token){$redirect_url = urlencode('http://1.dq095.applinzi.com/oauth2.php');$url = self::API_URL.'/sns/oauth2/refresh_token?appid='.APPID.'&grant_type=refresh_token&refresh_token='.$refresh_token;$content = curl_get( $url );$ret = json_decode($content, true );return self::getResult( $ret ) ? $ret : null;}/*** 通过OAuth2.0获取用户信息* @param type $access_token 网页授权接口调用凭证* @param type $openid 用户的唯一标识* @param type $lang 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语* @return type*/public static function getUserInfoByOAuth($access_token,$openid,$lang = 'zh_CN'){$redirect_url = urlencode('http://1.dq095.applinzi.com/oauth2.php');$url = self::API_URL.'/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang='.$lang;$content = curl_get( $url );$ret = json_decode($content, true );return self::getResult( $ret ) ? $ret : null;}