微信网页授权,微信登录,oauth2
Posted 上善若水-随
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信网页授权,微信登录,oauth2相关的知识,希望对你有一定的参考价值。
微信官方文档: http://mp.weixin.qq.com/wiki
微信公众平台OAuth2.0授权详细步骤如下:
1. 用户关注微信公众账号。
2. 微信公众账号提供用户请求授权页面URL。
3. 用户点击授权页面URL,将向服务器发起请求
4. 服务器询问用户是否同意授权给微信公众账号(scope为snsapi_base时无此步骤)
5. 用户同意(scope为snsapi_base时无此步骤)
6. 服务器将CODE通过回调传给微信公众账号
7. 微信公众账号获得CODE
8. 微信公众账号通过CODE向服务器请求Access Token
9. 服务器返回Access Token和OpenID给微信公众账号
10. 微信公众账号通过Access Token向服务器请求用户信息(scope为snsapi_base时无此步骤)
11. 服务器将用户信息回送给微信公众账号(scope为snsapi_base时无此步骤)
授权页面:oauth.php(引导用户点入...)
1 <?php 2 require(dirname(__FILE__) . ‘/api.class.php‘); 3 require(dirname(__FILE__) . ‘/wechat.class.php‘); 4 5 //多微信帐号支持 6 $weixinconfig = $db->getRow ( "SELECT * FROM " . $GLOBALS[‘ecs‘]->table(‘weixin_config‘) . " WHERE `id` = 1" ); 7 //多微信帐号支持 8 $id = intval($_GET[‘id‘]);//1 9 $oid = intval($_GET[‘oid‘]);//4 10 11 if($id > 0){ 12 $otherconfig = $db->getRow ( "SELECT * FROM " . $GLOBALS[‘ecs‘]->table(‘weixin_config‘) . " WHERE `id` = $id" ); 13 if($otherconfig){ 14 $weixinconfig[‘token‘] = $otherconfig[‘token‘]; 15 $weixinconfig[‘appid‘] = $otherconfig[‘appid‘]; 16 $weixinconfig[‘appsecret‘] = $otherconfig[‘appsecret‘]; 17 } 18 } 19 $weixin = new core_lib_wechat($weixinconfig); 20 if($_GET[‘code‘]){ 21 //echo $_GET[‘code‘];die(); 22 $json = $weixin->getOauthAccessToken(); 23 //print_r($json);exit(); 24 if($json[‘openid‘]){ 25 26 $wxuser = $GLOBALS[‘db‘]->getRow("select ecuid,unionid,uid,access_token from " . $GLOBALS[‘ecs‘]->table(‘weixin_user‘) . " where fake_id=‘{$json[‘openid‘]}‘"); 27 //print_r($wxuser);exit(); 28 //如果用户unionid多平台登录,自动绑定 29 if($wxuser[‘unionid‘]==""){ 30 $wxuserinof = $weixin->getUserInfo($json[‘openid‘]); 31 $sql = "update ".$GLOBALS[‘ecs‘]->table(‘weixin_user‘)." set `unionid`=‘".$wxuserinof[‘unionid‘]."‘ where uid=‘".$wxuser[‘uid‘]."‘"; 32 $GLOBALS[‘db‘]->query($sql); 33 } 34 $ecuid = $wxuser[‘ecuid‘]; 35 36 if($ecuid > 0){ 37 $username = $GLOBALS[‘db‘]->getOne("select user_name from ".$GLOBALS[‘ecs‘]->table(‘users‘)." where user_id=‘{$ecuid}‘"); 38 $GLOBALS[‘user‘]->set_session($username); 39 $GLOBALS[‘user‘]->set_cookie($username,1); 40 update_user_info(); //更新用户信息 41 recalculate_price(); //重新计算购物车中的商品价格 42 } 43 } 44 45 $url = $api->dir."/mobile/user.php"; 46 if($oid > 0){ 47 $url = $db->getOne ( "SELECT weburl FROM " . $GLOBALS[‘ecs‘]->table(‘weixin_oauth‘) . " 48 WHERE `oid` = $oid" ); 49 $db->query("update " . $GLOBALS[‘ecs‘]->table(‘weixin_oauth‘) . " 50 set click=click+1 WHERE `oid` = $oid "); 51 } 52 header("Location:$url");exit; 53 } 54 $url = $GLOBALS[‘ecs‘]->url()."/oauth.php?id={$id}&oid={$oid}"; 55 $url = $weixin->getOauthRedirect($url,1,‘snsapi_base‘); 56 header("Location:$url");exit;
以上是关于微信网页授权,微信登录,oauth2的主要内容,如果未能解决你的问题,请参考以下文章