just do it

OAuth2.0网页授权 不弹出“微信登录”页面 获得用户基本信息

通过OAuth2.0方式不弹出授权页面获得用户基本信息

1. 配置回调域名
2.构造请求url 获取code
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=http://mascot.duapp.com/oauth2.php&response_type=code&scope=snsapi_base&state=1#wechat_redirect

(scope=snsapi_base 表示应用授权作用域为 不弹出授权页面,直接跳转,只获取用户openid)

返回回调页面如下
http://israel.duapp.com?code=02a9bed29b2185a9f0ed3a48fe56e700&state=1

这里获得到了code

3.构造请求url 使用code获取OpenID
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

返回如下
{
“access_token”: “OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-oJ9VmO-0Z-_izfnSAX_s0aqDsYkW4s8W5dLZ4iyNj5Y6vey3dgDtFki5C8r6D0E6mSVxxtb8BjLMhb-mCyT_Yg”,
“expires_in”: 7200,
“refresh_token”: “OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-oJ9VmO-0Z-_izfnSAX_s0aqDsYkW4s8W5dLZ4iyNj5YBkF0ZUH1Ew8Iqea6x_itq13sYDqP1D7ieaDy9u2AHHw”,
“openid”: “oLVPpjqs9BhvzwPj5A-vTYAX3GLc”,
“scope”: “snsapi_base”
}

4.构造请求url 获取全局Access Token

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

返回如下
{
“access_token”: “NU7Kr6v9L9TQaqm5NE3OTPctTZx797Wxw4Snd2WL2HHBqLCiXlDVOw2l-Se0I-WmOLLniAYLAwzhbYhXNjbLc_KAA092cxkmpj5FpuqNO0IL7bB0Exz5s5qC9Umypy-rz2y441W9qgfnmNtIZWSjSQ”,
“expires_in”: 7200
}

5.构造请求url 使用全局ACCESS_TOKEN获取OpenID的用户详细信息
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID

返回如下
{
“subscribe”: 1,
“openid”: “oLVPpjqs2BhvzwPj5A-vTYAX4GLc”,
“nickname”: “lostphp”,
“sex”: 1,
“language”: “zh_CN”,
“city”: “北京”,
“province”: “北京”,
“country”: “中国”,
“headimgurl”: “http://wx.qlogo.cn/mmopen/JcDicrZBlREhnNXZRudod9PmibRkIs5K2f1tUQ7lFjC63pYHaXGxNDgMzjGDEuvzYZbFOqtUXaxSdoZG6iane5ko9H30krIbzGv/0”,
“subscribe_time”: 1386160805
}

成功获得用户基本信息

实例
[codee]
getRow(“SELECT * FROM `wxch_config` WHERE `id` = 1”);
$appid = $wxch_config[‘appid’];
$appsecret = $wxch_config[‘appsecret’];

// 获取code
if (!isset($_GET[‘code’])) {
// $redirect = urlencode($GLOBALS[‘ecs’]->url());
$redirect = ‘http://’.$_SERVER[‘HTTP_HOST’].$_SERVER[‘PHP_SELF’].’?’.$_SERVER[‘QUERY_STRING’];
$url = createOauthUrlForCode($redirect, $appid);

Header(“Location: $url”);
} else {
// 根据code 获取open_id
$code = $_GET[‘code’];

$openid = getOpenId($appid, $appsecret, $code);

if ($openid != ” && strlen($openid) == 28) {
$_SESSION[‘store_open_id’] = $openid;
//获取全局token
$access_token = getToken($appid, $appsecret);

//获取 用户信息
$user = getUser($access_token, $openid);
$_SESSION[‘store_user’] = $user;
}
}
}
}

/**
* 作用:生成可以获得code的url
*/
function createOauthUrlForCode($redirectUrl, $appid) {
$urlObj[“appid”] = $appid;
$urlObj[“redirect_uri”] = “$redirectUrl”;
$urlObj[“response_type”] = “code”;
$urlObj[“scope”] = “snsapi_base”;
$urlObj[“state”] = “STATE” . “#wechat_redirect”;
$bizString = formatBizQueryParaMap($urlObj, false);
return “https://open.weixin.qq.com/connect/oauth2/authorize?” . $bizString;
}

/**
* 作用:生成可以获得openid的url
*/
function createOauthUrlForOpenid($appid, $secret, $code) {
$urlObj[“appid”] = $appid;
$urlObj[“secret”] = $secret;
$urlObj[“code”] = $code;
$urlObj[“grant_type”] = “authorization_code”;
$bizString = formatBizQueryParaMap($urlObj, false);
return “https://api.weixin.qq.com/sns/oauth2/access_token?” . $bizString;
}
/**
* 作用:生成可以获得全局Access Token的url
*/
function createOauthUrlForToken($appid, $secret) {
$urlObj[“appid”] = $appid;
$urlObj[“secret”] = $secret;
// $urlObj[“code”] = $code;
$urlObj[“grant_type”] = “client_credential”;
$bizString = formatBizQueryParaMap($urlObj, false);
return “https://api.weixin.qq.com/cgi-bin/token?” . $bizString;

}
/**
* 作用:生成可以获得用户信息的url
*/
function createOauthUrlForUser($token, $openid) {
$urlObj[“access_token”] = $token;
$urlObj[“openid”] = $openid;
$bizString = formatBizQueryParaMap($urlObj, false);
return “https://api.weixin.qq.com/cgi-bin/user/info?” . $bizString;

}

/**
* 作用:通过curl向微信提交code,以获取openid
*/
function getOpenid($appid, $secret, $code) {
$url = createOauthUrlForOpenid($appid, $secret,$code);
//初始化curl
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//运行curl,结果以jason形式返回
$res = curl_exec($ch);
curl_close($ch);
//取出 openid
$data = json_decode($res, true);
$openid = $data[‘openid’];
return $openid;
}

/**
* 作用:通过curl向微信提交openid,以获取获取全局Access Token
*/
function getToken($appid, $secret) {
$url = createOauthUrlForToken($appid, $secret);

//初始化curl
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//运行curl,结果以jason形式返回
$res = curl_exec($ch);
curl_close($ch);
//取出token
$data = json_decode($res, true);
$access_token = $data[‘access_token’];
return $access_token;
}
/**
* 作用:通过curl向微信提交openid tolen,以获取用户信息
*/
function getUser($token,$openid) {
$url = createOauthUrlForUser($token,$openid);
//初始化curl
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//运行curl,结果以jason形式返回
$res = curl_exec($ch);
curl_close($ch);
//取出用户信息
$data = json_decode($res, true);

return $data;
}

/**
* 作用:格式化参数,签名过程需要使用
*/
function formatBizQueryParaMap($paraMap, $urlencode) {
$buff = “”;
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if ($urlencode) {
$v = urlencode($v);
}
//$buff .= strtolower($k) . “=” . $v . “&”;
$buff .= $k . “=” . $v . “&”;
}
$reqPar;
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) – 1);
}
return $reqPar;
}
/* 检查是否是微信浏览器访问 */
function is_wechat_browser(){
$user_agent = $_SERVER[‘HTTP_USER_AGENT’];
if (strpos($user_agent, ‘MicroMessenger’) === false){
return false;
} else {
return true;
}
}
?>
[/codee]

点赞