一、这个文件微信授权使用的是OAuth2.0授权的方式。主要有以下简略步骤:

  第一步:判断有没有code,有code去第三步,没有code去第二步

  第二步:用户同意授权,获取code

  第三步:通过code换取网页授权access_token

  第四步:使用access_token获取用户信息

  https://github.com/jijinduoduo/GetWxUser

二、代码GetWxUser.php

 <?php
/**
* 获取微信用户信息
* @author: Lucky hypo
*/
class GetWxUser{
private $appid = '';
private $appsecret = '';
/**
* 1、获取微信用户信息,判断有没有code,有使用code换取access_token,没有去获取code。
* @return array 微信用户信息数组
*/
public function get_user_all(){
if (!isset($_GET['code'])){//没有code,去微信接口获取code码
$callback = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];//微信服务器回调url,这里是本页url
$this->get_code($callback);
} else {//获取code后跳转回来到这里了
$code = $_GET['code'];
$data = $this->get_access_token($code);//获取网页授权access_token和用户openid
$data_all = $this->get_user_info($data['access_token'],$data['openid']);//获取微信用户信息
return $data_all;
}
}
/**
* 2、用户授权并获取code
* @param string $callback 微信服务器回调链接url
*/
private function get_code($callback){
$appid = $this->appid;
$scope = 'snsapi_userinfo';
$state = md5(uniqid(rand(), TRUE));//唯一ID标识符绝对不会重复
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . urlencode($callback) . '&response_type=code&scope=' . $scope . '&state=' . $state . '#wechat_redirect';
header("Location:$url");
}
/**
* 3、使用code换取access_token
* @param string 用于换取access_token的code,微信提供
* @return array access_token和用户openid数组
*/
private function get_access_token($code){
$appid = $this->appid;
$appsecret = $this->appsecret;
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
$user = json_decode(file_get_contents($url));
if (isset($user->errcode)) {
echo 'error:' . $user->errcode.'<hr>msg :' . $user->errmsg;exit;
}
$data = json_decode(json_encode($user),true);//返回的json数组转换成array数组
return $data;
}
/**
* 4、使用access_token获取用户信息
* @param string access_token
* @param string 用户的openid
* @return array 用户信息数组
*/
private function get_user_info($access_token,$openid){
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
$user = json_decode(file_get_contents($url));
if (isset($user->errcode)) {
echo 'error:' . $user->errcode.'<hr>msg :' . $user->errmsg;exit;
}
$data = json_decode(json_encode($user),true);//返回的json数组转换成array数组
return $data;
}
}
?>

不足之处,请留言告知,谢谢!

最新文章

  1. 关于css3的背景渐变
  2. mysql_fetch_array,mysql_fetch_row,mysql_fetch_assoc区别
  3. Linux启动过程详解
  4. laravel5 使用model 表名总是多个s
  5. PHP代码实用片段
  6. css 超过宽度显示...
  7. x01.os.9: 进程切换
  8. 深入理解Android的startservice和bindservice
  9. BZOJ 4031: [HEOI2015]小Z的房间 Matrix-Tree定理
  10. mysql sql语句使用技巧
  11. tar、zip 、unzip 打包与压缩 (参考:http://pengyl.blog.51cto.com/5591604/1191197)
  12. mongodb与mysql相比的优缺点
  13. OAuth2.0认证过程
  14. DevOps之基础设施
  15. union 的两个用处
  16. List-ArrayList集合基础增强底层源码分析
  17. .NetCore多文件上传进度的示例
  18. DEA快速生成get&amp;set方法
  19. 通过Tag标签回退版本修复bug
  20. Python 3 进阶 —— print 打印和输出

热门文章

  1. OpenSSL version mismatch. Built against 1000105f, you have 10001060
  2. 下拉列表 Spinner
  3. 解决获取View的width和Height为0的4种方法
  4. Django基于Pycharm开发之二 [使用django adminSite]
  5. static_cast 、const_cast、dynamic_cast、reinterpret_cast 关键字简单解释
  6. Freemaker模板指令
  7. luogu3390 【模板】矩阵快速幂
  8. dependency or constituency
  9. [uiautomator篇][9]遇到问题
  10. linux下 export只能设定临时变量