微信oauth2.0和自定义菜单携带openid

来源:互联网 发布:杭州汉聚网络骚扰电话 编辑:程序博客网 时间:2024/05/03 11:32

做了一个普通的首页网站是通过账号密码来进行登录的,看到别的网站可以通过在微信中直接打开而不用输入账号密码,且是和微信绑定的,虽然不知道这是怎么实现的,自己想了下通过点击微信的自定义菜单时候携带上自己的openid,在用户表找下对应的openid,如果存在就自动创建用户session如果不存在则认为未登录,不知道这种思路对不对。


微信授权获取用户openid和用户基本信息,页面获取openid主要用到微信的oAuth2.0网页授权,且具备该权限,认证的服务号。


授权有两种方式:

1、无需用户确认(悄悄模式) snsapi_base 

2、需要用户确认 snsapi_userinfo


授权步骤:

1、通过页面点击"click"事件 触发或者放入自定义菜单url(貌似不能通过curl触发不然CODE好像没法传给回调URL接收)两种授权方式的URL链接给回调地址传递CODE;

2、通过CODE、APPID、APPSECRET获取用户access_token凭证(和以往access_token不是一回事儿 只是名字一样而已);

3、通过access_token获取用户openid (如果是snsapi_base模式到此就已经完成)

4、通过access_token、APPID获取用户基本信息(snsapi_userinfo模式到此完成)


封装获取用户openid和userinfo信息类:

<?php/*** oAuth2.0网页授权** 1、具有微信的oAuth2.0权限 认证的服务号* 2、配置oAuth2.0的回调域名 不带http or https *  配置回调域名注意:www.sxfenglei.com 和 sxfenglei.com是2个不同的域 www.sxfenglei.com并不属于sxfenglei.com  * @author sxfenglei* @email sxfenglei@vip.qq.com** 事例:* require_once 'WxOauth.class.php';* define('APPID','xxxxxx');* define('SECRET','xxxxxx');* define('CODE',$_GET['code']);* $wx = new WxOauth(APPID,SECRET,CODE);* //$res = $wx->getOpenid();* $res = $wx->getUserinfo();* var_dump($res);*/class WxOauth{private $appid;private $appsecret; private $code;private $access_token;private $openid; private $userinfo; /*** 初始化 需要APPID 、 APPSECRET 和 CODE* 特别注意这个CODE是通过页面“点击”触发 get请求后传递给回调的 好像不能通过curl get触发*/public function __construct($appid,$appsecret,$code){if(empty($appid) || empty($appsecret)||empty($code)){die('init fail');}//appid and appsecret$this->appid = $appid;$this->appsecret = $appsecret;//codeif(empty($code)){ die('parameter error');}$this->code = $code;//cURLrequire_once 'function.class.php';} /** post 获取openid* 获取 CODE有两种方式:* snsapi_base不用用户“确认”但只能获取openid* snsapi_userinfo 需要用户“确认”但可以获取openid 和 用户信息*/public function getOpenid($scope="snsapi_base"){   //code换取access_token$url = "https://api.weixin.qq.com/sns/oauth2/access_token?";$arr = array('appid'=>$this->appid,'secret'=>$this->appsecret,'code'=>$this->code,'grant_type'=>'authorization_code');$dataArr = json_decode(postCurl($url,$arr),true);$this->access_token = $dataArr['access_token'];$this->openid = $dataArr['openid'];if($scope=="snsapi_base"){return $dataArr['openid'];  }else{return $dataArr;  }}/*** get 获取userinfo*/public function getUserinfo(){//code获取access_token票据$res = $this->getOpenid("snsapi_userinfo");$this->access_token = $res['access_token'];//access_token换取用户信息/*$url = "https://api.weixin.qq.com/sns/userinfo?";$postArr = array('access_token'=>$this->access_token,'openid'=>$this->openid,'lang'=>'zh_CN'); $this->userinfo = json_decode(postCurl($url,$postArr),true);*/$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$this->access_token."&openid=".$this->openid."&lang=zh_CN";$this->userinfo = json_decode(getCurl($url),true);return $this->userinfo;}/*** get 验证access_token是否有效*/public function isValidAccessToken($access_token){if(empty($access_token)){die('The access_token cannot be empty');}$url = "https://api.weixin.qq.com/sns/auth?access_token=".$access_token."&openid=".$this->openid; $res = json_decode(getCurl($url),true);if($res['errcode'] == 0){return true;}else{return false;}}/*** post 刷新access_token*/public function refreshAccessToken($refresh_token){if(empty($refresh_token)){die('The refresh_token cannot be empty');}//$url = "appid=".$this->openid."&grant_type=refresh_token&refresh_token=".$refresh_token; //$res = json_decode(getCurl($url),true); $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?";$arr = array('appid'=>$this->appid,'grant_type'=>'refresh_token','refresh_token'=>$refresh_token);$res = json_decode(postCurl($url,$arr),true); return $res; }} ?>

用法:

首先通过点击超链接传递CODE给回调页 来获取CODE 不能用curl因为其中包含有回调URL

<?php//引入类require_once 'WxOauth.class.php';//该回调页 获取codeif(!isset($_GET['code']) && empty($_GET['code'])){echo '用户未授权';exit;} define('APPID','填写你自己的APPID');  //需要认证的服务号define('SECRET','填写你自己的APPSECRET');define('CODE',$_GET['code']);$wx = new WxOauth(APPID,SECRET,CODE);//$res = $wx->getOpenid();  //获取openid$res = $wx->getUserinfo();  //获取userinfovar_dump($res);?>




页面获取openid主要用到微信的oAuth2.0网页授权,且具备该权限,认证的服务号。
0 1