给没有公众号的网站实现网页授权登入很不错 <?php <br />
// +----------------------------------------------------------------------<br>
// | OneThink [ WE CAN DO IT JUST THINK IT ]<br>
// +----------------------------------------------------------------------<br>
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.<br>
// +----------------------------------------------------------------------<br>
// | Author: 艺品网络 <br>
// +---------------------------------------------------------------------- <br>
namespace Home\Controller;<br>
use Think\Controller;<br>
/**<br>
* 公众号管理<br>
*/ <br>
class WebauthController extends Controller { <br>
/*id 公众号ID<br>
*url 跳转地址<br>
*scope授权方式 false静默授权<br>
*/<br>
public function index(){<br>
// $map ['id'] = '114';//I ( 'id', 0, 'intval' );<br>
// $info = M ( 'member_public' )->where ( $map )->find (); <br>
$info=array(<br>
'appid' => , <br>
'appsecret' => , <br>
);<br>
<br>
$redirect_url=U('Webauth/index');//回调地址<br>
$code=I('get.code');<br>
$scope=I('get.scope');<br>
if(empty($scope)){<br>
$scope=false;<br>
}else{<br>
$scope=true;<br>
}<br>
if(empty($code)){ <br>
$url='http://ky.xebox.cn/index.php/Index/Index/index/';//传送地址<br>
Yoauth($info['appid'],$redirect_url,$scope,$url);<br>
}<br>
$data=Yoauth_access_token($info['appid'],$info['secret'],$code); <br>
$canshu=http_build_query($data); <br>
echo $URL=I('get.state').'?'.$canshu;<br>
redirect($URL);<br>
} <br>
} <br>
/**********************************微信网页授权*****************************************/<br>
<br>
/* 第一步:用户同意授权,获取code <br>
$appid;//公众号appid<br>
$redirect_url;//授权后重定向的回调链接地址,请使用urlencode对链接进行处理<br>
$response_type;//返回类型,请填写code<br>
$scope;//应用授权作用域,snsapi_base不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)<br>
$state;//重定向后会带上state参数,可以填写公众号ID判断用户来自哪个公众号<br>
*/ <br>
function Yoauth($appid,$redirect_url,$scope=true,$state='ypwl',$response_type="code"){ <br>
if($scope){<br>
$scope='snsapi_userinfo';//弹出网页授权<br>
}else{<br>
$scope='snsapi_base';//静默授权<br>
}<br>
$map ['appid'] = $appid;<br>
$map ['redirect_uri'] = $redirect_url;<br>
$map ['response_type'] = $response_type;<br>
$map ['scope'] = $scope;<br>
$map ['state'] = $state;<br>
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query ( $map ) . '#wechat_redirect';<br>
redirect($url); <br>
}<br>
/* 第二步:通过code换取网页授权access_token 并且获取用户信息省略后面的步骤<br>
注意:如果是静默方式到这一步就算完成了 直接返回用户的openid snsapi_base式的网页授权流程即到此为止。<br>
appid 是 公众号的唯一标识<br>
secret 是 公众号的appsecret<br>
code 是 填写第一步获取的code参数<br>
grant_type 是 填写为authorization_code <br>
*/<br>
function Yoauth_access_token($appid,$secret,$code,$grant_type='authorization_code'){ <br>
$map ['appid'] = $appid;<br>
$map ['secret'] = $secret;<br>
$map ['code'] = $code;<br>
$map ['grant_type'] = $grant_type; <br>
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?'. http_build_query ( $map ) ;<br>
$content = file_get_contents ( $url );<br>
$content = json_decode ( $content, true );<br>
if (! empty ( $content ['errmsg'] )) {<br>
exit ( $content ['errcode'].'--'.$content['errmsg'] );<br>
}<br>
//判断授权方式<br>
if($content['scope'] == 'snsapi_userinfo'){<br>
$content=Yoauth_userinfo($content['access_token'],$content['openid']);//获取微信用户信息<br>
}<br>
return $content;<br>
}<br>
/*<br>
第三步:刷新access_token(如果需要)<br>
*/<br>
/* 第四步:拉取用户信息(需scope为 snsapi_userinfo)<br>
access_token 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同<br>
openid 用户的唯一标识<br>
lang 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语<br>
*/<br>
function Yoauth_userinfo($access_token,$openid,$lang='zh_CN'){ <br>
$map ['access_token'] = $access_token;<br>
$map ['openid'] = $openid;<br>
$map ['lang'] = $lang; <br>
$url = 'https://api.weixin.qq.com/sns/userinfo?'. http_build_query ( $map );<br>
$content = file_get_contents ( $url );<br>
$content = json_decode ( $content, true );<br>
if (! empty ( $content ['errmsg'] )) {<br>
exit ( $content ['errcode'].'--'.$content['errmsg'] );<br>
}<br>
return $content;<br>
} <br>
/* 用户openid获取用户信息<br>
access_token 是 调用接口凭证<br>
openid 是 普通用户的标识,对当前公众号唯一<br>
lang 否 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语<br>
*/<br>
function Yget_wx_userinfo($openid,$access_token,$lang='zh_CN'){<br>
$map ['access_token'] = $access_token;<br>
$map ['openid'] = $openid;<br>
$map ['lang'] = $lang; <br>
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?'. http_build_query ( $map );<br>
$content = file_get_contents ( $url );<br>
$content = json_decode ( $content, true );<br>
if (! empty ( $content ['errmsg'] )) {<br>
exit ( $content ['errcode'].'--'.$content['errmsg'] );<br>
}<br>
return $content;<br>
}