function testAuthorize()
{
global $appid;
$auth = Oauth2::getInstance();
$redirect_url = 'http://www.youdomain.com/mpcube/test/Wechat/Publics/web.php?_ijt=du8vh3rtpbi64dmhf87b9952qs';
//只想获取openid,静默
//$auth->authorize($appid, $redirect_url, Oauth2Scope::snsapi_base, array('im'=>'state'));
//希望获取用户详细信息,需用户授权
$auth->authorize($appid, $redirect_url, Oauth2Scope::snsapi_userinfo, array('im'=>'state'));
}
function testGetOpenid($code)
{
global $appid, $appsecret;
//获取并缓存网页access_token
$auth = Oauth2::getInstance();
$ret = $auth->setCacheDriver(Oauth2::CACHE_DRIVER_FILESYSTEM)->setFilePath()->getFullWebAccessToken($appid, $appsecret, $code);
var_dump($ret);
//后续获得openid后逻辑处理
$redirecturl = 'https://www.youdomain.com/index.html?abc=def';
$state = json_decode(urldecode($_GET['state']), true);
$statestr = http_build_query($state);
$openid = $ret['openid'];
//此处为跳转至下一个页面处理
var_dump("{$redirecturl}&{$statestr}&openid={$openid}");
header("location:{$redirecturl}&{$statestr}&openid=".$openid);
exit();
}
if(empty($_GET['code'])) {
testAuthorize();
} else {
testGetOpenid($_GET['code']);
}
$auth = Oauth2::getInstance();
$refresh_token = '';
$ret = $auth->refreshToken($appid, $refresh_token);
var_dump($ret);
$auth = Oauth2::getInstance();
$web_access_token = '';
$openid = '';
$ret = $auth->getUserinfo($web_access_token, $openid);
var_dump($ret);
$auth = Oauth2::getInstance();
$web_access_token = '';
$openid = '';
$ret = $auth->auth($web_access_token, $openid);
var_dump($ret);