php实现微信网站获取用户信息的功能
2017-03-24 09:14
781 查看
php实现微信网站获取用户信息的功能,服务号获取信息经测试没得问题,订阅号这个未测试,希望测试过了的朋友能够回复指导一下。
<?php use think\Controller; use think\Db; /** * 作者 http://www.5atl.com * */ class Weilogin extends Controller { /** * 参考资料详见 https://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html */ /** * 订阅号 */ // private $appid = 'wx149978cf463*****e'; // private $secret = '33f0d0314477c77fdd8d85***f'; /** * 服务号 */ private $appid = 'wx9916a5a3d3e*****c'; private $secret = 'e6d756b57d54d27cc03****17e63'; private $token = ''; private $openid = ''; private $reflash = ''; /** * 授权接口 * * @param unknown $appid * @param unknown $jumpto */ public function index() { $weichatflat = true;'http://***********'; // 放获取微信信息的跳转链接 $bind = input('bind'); // 用作绑定还是登录 $url = ''; if ($weichatflat == true) { // 服务号使用 $jumpto = 'http://**********/weilogin/getinfo?bind=' . $bind; $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->appid . '&redirect_uri=' . $jumpto . '&response_type=code&scope=snsapi_userinfo&state=' . $bind . '#wechat_redirect'; } else { // 订阅号服务号都可以使用 $jumpto = 'http://**********/weilogin/getopenid?bind=' . $bind; $url = $weichatflat . '/get-weixin-code.html?appid=' . $this->appid . '&redirect_uri=' . $jumpto . '&response_type=code&scope=snsapi_base&state=' . $bind . '#wechat_redirect'; } return redirect($url); } /** * 获取微信id */ public function getOpenid() { $bind = input('bind'); // 用作绑定还是登录 $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->secret . '&code=' . input('code') . '&grant_type=authorization_code'; $json = file_get_contents($url); $json = json_decode($json, true); return $this->bindUserBase($json, $bind); } /** * 订阅号登录或绑定 * @param unknown json带的值openid * @param unknown 绑定还是登录 * @return \think\response\Redirect */ public function bindUserBase($json, $bind) { if ($bind == 0) { $hasUser = Db::name('**************')->where('wxid', $json['openid'])->find(); session('oa_cid', $hasUser['cid']); // 公司id session('oa_uid', $hasUser['mid']); // 用户ID session('oa_username', $hasUser['username']); // 用户名 session('oa_portrait', $hasUser['face']); // 用户头像 session('oa_pmid', $hasUser['pmid']); // 上级id return redirect(url('index/index')); } else { $mid = session('oa_uid'); $param['wxid'] = $json['openid']; Db::name('************')->save($param, [ 'mid' => $mid ]); $this->success('绑定微信成功', url('index/index')); } } /** * 根据公众号appid获取用户的基本信息 * * @param unknown $appid * @return mixed */ public function getInfo() { $bind = input('bind'); // 用作绑定还是登录 // 1. 拿到token $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->secret . '&code=' . input('code') . '&grant_type=authorization_code'; $reback = file_get_contents($url); $json = json_decode($reback, true); $this->token = $json['access_token']; $this->openid = $json['openid']; $this->reflash = $json['refresh_token']; // 2. token是否过期 // $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $this->token . '&openid=' . $this->openid; // $reback = file_get_contents($url); // $json = json_decode($reback, true); // 3. token过期后重新拿刷新,跟步骤1一样的结果 // if (array_key_exists('errcode', $json) && $json['errcode'] == 40001) { // $url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . $this->appid . '&grant_type=refresh_token&refresh_token=' . $this->reflash; // $reback = file_get_contents($url); // $json = json_decode($reback, true); // // 新的token和openid // $this->token = $json['access_token']; // $this->openid = $json['openid']; // $this->reflash = $json['refresh_token']; // } // 4. 拿详细信息了 $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $this->token . '&openid=' . $this->openid . '&lang=zh_CN'; $reback = file_get_contents($url); $json = json_decode($reback, true); if (array_key_exists('errcode', $json)) { $this->error($reback . '<br/>token=' . $this->token . '<br/>openid=' . $this->openid, null, null, 10); } // 5.绑定信息获登录 return $this->bindUserInfo($json, $reback, $bind); } /** * 服务号登录还是绑定 * @param unknown $json openid, * @param unknown $reback * @param unknown $bind * @return \think\response\Redirect */ public function bindUserInfo($json, $reback, $bind) { if ($bind == 0) { $hasUser = Db::name('********')->where('wxid', $json['openid'])->find(); if ($hasUser == null) { $this->error('对不起,您的账号还未绑定微信,请使用OA账号的鞥路', url('login/index')); } session('oa_cid', $hasUser['cid']); // 公司id session('oa_uid', $hasUser['mid']); // 用户ID session('oa_username', $hasUser['username']); // 用户名 session('oa_portrait', $hasUser['face']); // 用户头像 session('oa_pmid', $hasUser['pmid']); // 上级id return redirect(url('index/index')); } else { $mid = session('oa_uid'); $param['face'] = $json['headimgurl']; $param['wxid'] = $json['openid']; $param['wxinfo'] = $reback; Db::name('********')->where('mid', $mid)->update($param); $this->success('绑定微信成功', url('index/index')); } } } ?>
相关文章推荐
- 微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法
- 微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法
- PHP+TP框架实现微信网页授权,获取用户信息
- php 微信开发获取用户信息如何实现
- php实现微信模拟登陆、获取用户列表及群发消息功能示例
- php 微信开发获取用户信息如何实现
- php 实现微信模拟登陆、获取用户列表及群发消息功能示例
- NodeJs实现自定义分享功能,获取微信授权+用户信息
- 微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法.
- 微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法.
- PHP版微信第三方实现一键登录及获取用户信息的方法
- PHP+TP框架实现微信网页授权,获取用户信息
- 浅谈PHP实现微信获取周边信息功能
- php利用微信接口获取微信用户信息
- 监听器实现网站计数并获取用户信息
- Asp.NET MVC 使用 SignalR 实现推送功能二(Hubs 在线聊天室 获取保存用户信息)
- 利用servlet 实现JAVAWeb访问微信OAuth2.0认证,获取用户信息的实例
- 微信订阅号里实现oauth授权登录,并获取用户信息 (完整篇)