三、网页授权获取用户信息
来源:互联网 发布:上色软件 编辑:程序博客网 时间:2024/04/30 10:14
网页获取用户信息:
1. 首先 将 auth2 的 可信域名 改成 你即将调用的 返回用户 信息的 域名
这样 才不会出现 redirect_uri 参数错误
2、 接下来 获取code
/** * @todo auth2 跳转获取 code * @param null * @return null 跳转 */ public function authqy(){ $params = array( 'appid' => $this->_appid, //企业号appid 'redirect_uri' => site_url('home/index/getinfo'), //获取code的地址 'response_type' => 'code', //固定 'scope' => 'snsapi_base', //固定 'state' => 'wjw', //任意字符串 ); $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?'; $url = $url . http_build_query($params) .'#wechat_redirect'; echo '<script> location.href = "'.$url.'";</script>'; exit; }
3、根据code 获取用户信息 获取token的时候 如果不知道 secret (请参考 笔记二)
/** * @todo auth2 跳转获取 code * @param null * @return null 跳转 */ public function authqy(){ $params = array( 'appid' => $this->_appid, //企业号appid 'redirect_uri' => site_url('home/index/getinfo'), //获取code的地址 'response_type' => 'code', //固定 'scope' => 'snsapi_base', //固定 'state' => 'wjw', //任意字符串 ); $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?'; $url = $url . http_build_query($params) .'#wechat_redirect'; echo '<script> location.href = "'.$url.'";</script>'; exit; } /** * @todo 根据code 获取用户信息 * @param string $code get方式 微信回调参数code * @return null */ public function getinfo(){ $code = $this->input->get('code'); if($code){ $token = $this->get_token(); $url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?'; $params = array( 'access_token' => $token, 'code' => $code, ); $this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false)); $url = $url . http_build_query($params); $res = $this->curl->simple_get($url); $info = json_decode($res); if(isset($info->DeviceId)){ $data = array(); $data['userid'] = isset($info->UserId)?$info->UserId:''; //企业成员id $data['openid'] = isset($info->OpenId)?$info->OpenId:'';//非企业成员 openid $data['deviceid'] = $info->DeviceId; //手机微信设备号 删除重装会更新 否则不会改变 //这里获取到了 用户信息 var_dump($data); exit; }else{ die("微信返回有误!".$res); } }else{ die("微信返回有误!"); } } /** * @todo 获取公众号token * @param null * @return string $token */ public function get_token(){ $tokenstr = file_get_contents('./token.txt'); if($tokenstr){ $tokenarr = explode(',', $tokenstr); if($tokenarr[1] + 7200 > time()){ return $tokenarr[0]; } //否则token失效 }//否则token为空 //重新获取token $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?'; $params = array( 'corpid' => $this->_appid, 'corpsecret' => $this->_appsecret, ); $url = $url . http_build_query($params); $this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false)); $res = $this->curl->simple_get($url); $result = json_decode($res); if(isset($result->access_token)){ file_put_contents('./token.txt', $result->access_token.','.time()); return $result->access_token; }else{ die('token 获取失败!'); } }
注:这里如果获取的
userid 不为空 则为关注过企业号的
openid 不为空 则为未关注企业号的
0 0
- 三、网页授权获取用户信息
- 网页授权获取用户信息
- 支付宝网页授权获取用户信息 网页获取用户信息的
- 支付宝:网页授权获取用户信息
- 微信网页授权—获取用户信息
- 微信网页授权(获取用户信息)
- 微信网页授权后台获取用户信息
- 微信开发-网页授权获取用户信息
- 支付宝 网页授权 获取用户信息
- 支付宝 网页授权 获取用户信息
- 微信网页授权及获取用户信息
- java微信网页授权获取用户信息以及JSSDK自定义分享等功能<三>
- 微信网页开发之网页授权获取用户信息
- 微信公众号-获取用户信息(网页授权获取)
- 支付宝二(网页授权获取用户信息)
- PHP---微信OAuth2.0网页授权获取用户信息
- PHP+TP框架实现微信网页授权,获取用户信息
- 微信公众号获取用户信息-网页授权
- SharedPreferences详解
- RHCS+Conga+iSCSI+CLVM+GFS实现Web服务的共享存储HA集群
- SoapUI脚本保存变量,加一BUG
- 系统当前时间
- 给CentOS6.3 + PHP5.3 安装PHP性能测试工具 XHProf-0.9.2
- 三、网页授权获取用户信息
- arm64 死在memcpy中:Unhandled fault: alignment fault
- java链表的get方法遍历VS使用迭代器遍历
- Android studio 启动不检查SDK
- ADO.NET 结构
- textview水平滚动
- SVN---回退功能
- A星算法 OC实现具体代码
- Maven