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

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

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

  第三步:拉取用户信息(需scope为 snsapi_userinfo)

  微信网页授权开发文档请看官网:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

二、我这里直接出代码了,一共2个文件。

//其它文件调用UserInfo.php的时候注意namespace。
use wx\userInfo\UserInfo;
include 'UserInfo.php';
$wx = new UserInfo();
$data = $wx->get_user_all();

1配置文件config.php

 <?php
namespace wx\wxConfig;
/**
* 微信请求相关配置类
*/
class ConfigTool{
/**
* 微信配置参数
* @return array 配置参数
*/
public function Config(){
// appID
$config['appid'] = '';
// appSecret
$config['appsecret'] = '';
// 微信回调链接地址(本页)
$config['redirect_uri'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
// 用户授权并获取code的url地址
$config['authorize_url'] = 'https://open.weixin.qq.com/connect/oauth2/authorize';
// 获取openid和access_toke的url地址
$config['access_token_url'] = 'https://api.weixin.qq.com/sns/oauth2/access_token';
// 获取拉取用户信息(需scope为 snsapi_userinfo)的url地址
$config['userinfo_url'] = 'https://api.weixin.qq.com/sns/userinfo';
// 本文件夹所在的url路径
$config['self_path'] = 'http://'.dirname($_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); return $config;
}
}
?>

2.获取用户信息文件UserInfo.php

 <?php
namespace wx\userInfo;
use wx\wxConfig\ConfigTool;
include 'config.php';
/**
* 获取微信用户信息
* $wx = new UserInfo();
* $data = $wx->get_user_all();
*/
class UserInfo{ private $weixin_config = array();
public function __construct() {
$re = new ConfigTool;
$this->weixin_config = $re->Config(); //载入配置文件
}
/**
* 获取微信用户信息
* @return array 微信用户信息数组
*/
public function get_user_all(){
if (!isset($_GET['code'])){//没有code,去微信接口获取code码
$callback = $this->weixin_config['redirect_uri'];//服务器返回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;
}
} /**
* 1、用户授权并获取code
* @param string $callback 微信服务器回调链接url
*/
private function get_code($callback){
$appid = $this->weixin_config['appid'];
$scope = 'snsapi_userinfo';//snsapi_base只能获取access_token和openID,snsapi_userinfo可以获取更详细的用户资料,比如头像、昵称、性别等
$state = md5(uniqid(rand(), TRUE));//唯一ID标识符绝对不会重复
$url = $this->weixin_config['authorize_url'].'?appid=' . $appid . '&redirect_uri=' . urlencode($callback) . '&response_type=code&scope=' . $scope . '&state=' . $state . '#wechat_redirect';
header("Location:$url");
} /**
* 2、使用code换取access_token
* @param string 用于换取access_token的code,微信提供
* @return array access_token和用户openid数组
*/
private function get_access_token($code){
$appid = $this->weixin_config['appid'];
$appsecret = $this->weixin_config['appsecret'];
$url = $this->weixin_config['access_token_url'].'?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;
} /**
* 3、使用access_token获取用户信息
* @param string access_token
* @param string 用户的open id
* @return array 用户信息数组
*/
private function get_user_info($access_token,$openid){
$url = $this->weixin_config['userinfo_url'].'?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;
} } ?>

 三、觉得两个文件多,也可以用一个文件类封装。场景不同,喜欢哪个用哪个。

http://www.cnblogs.com/hiit/p/8669361.html

PHP微信公众平台oauth2.0网页授权登录类的封装demo的更多相关文章

  1. 微信公众平台OAuth2.0网页授权

    微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使用这个的时候失败了或者无法理解其内容,希望我出个教程详细讲解一下,于是便有了这篇文章. 一. ...

  2. PHP微信公众平台OAuth2.0网页授权,获取用户信息代码类封装demo(二)

    一.这个文件微信授权使用的是OAuth2.0授权的方式.主要有以下简略步骤: 第一步:判断有没有code,有code去第三步,没有code去第二步 第二步:用户同意授权,获取code 第三步:通过co ...

  3. php 微信公众平台OAuth2.0网页授权,获取用户信息代码类封装demo

    get_wx_data.php <?php /** * 获取微信用户信息 * @author: Lucky hypo */ class GetWxData{ private $appid = ' ...

  4. 微信公众平台oauth2.0网页授权参考资料

    http://www.wangwenxiao.com/weixin/wxgzptoauth2_0wysq_12.html

  5. C#-MVC开发微信应用(2)--OAuth2.0网页授权

    微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使用这个的时候失败了或者无法理解其内容,希望我出个教程详细讲解一下,于是便有了这篇文章. 一. ...

  6. 用Chrome查看微信访问需要OAuth2.0网页授权的页面

    在PC浏览器打开某网站页面提示页面错误,是因为进行了OAuth2.0网页授权 有以下限制, 1.必须在微信打开 2.微信页面授权 其中第一步比较容易实现,修改下ua(user-agent),让其携带“ ...

  7. 微信公众平台OAuth2.0授权

    1. 配置授权回调页面域名 进入微信公众平台后台后,依次进入开发者中心-权限表,找到网页授权获取用户基本信息,点击右侧的修改. 授权回调域名配置规范为全域名并且不带http,比如需要网页授权的域名为: ...

  8. 微信公众号开发(5)---使用开源组件开发公众号OAuth2.0网页授权授权登录

    搞清微信公众号授权登录的步骤步骤,我们的开发就完成了一大步 献上github 地址: https://github.com/Wechat-Group/weixin-java-tools/wiki/MP ...

  9. 微信公众平台开发(71)OAuth2.0网页授权

    微信公众平台开发 OAuth2.0网页授权认证 网页授权获取用户基本信息 作者:方倍工作室 微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使 ...

随机推荐

  1. 在CYGWIN下编译和运行软件Bundler ,以及PMVS,CMVS的编译与使用

    本人按照 http://blog.csdn.net/zzzblog/article/details/17166869 http://oliver.zheng.blog.163.com/blog/sta ...

  2. FusionCharts报错

    1.具体报错如下 SCRIPT 5007:无法获取属性"SetReturnValue"的值: 对象为空或未定义 script block(158),行1字符158 2.错误原因 3 ...

  3. Column 'id' in where clause is ambiguous

    1.错误描述 org.hibernate.exception.ConstraintViolationException: error executing work at org.hibernate.e ...

  4. C#扩展方法类库StringExtensions

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. tomcat原理(二)

    一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命令的用法如下:

  6. SOA和微服务架构

    微服务架构强调的第一个重点就是业务系统需要彻底的组件化和服务化,原有的单个业务系统会拆分为多个可以独立开发,设计,运行和运维的小应用.这些小应用之间通过服务完成交互和集成.每个小应用从前端web ui ...

  7. Python Cookbook(第3版)中文版:15.18 传递已打开的文件给C扩展

    15.18 传递已打开的文件给C扩展¶ 问题¶ 你在Python中有一个打开的文件对象,但是需要将它传给要使用这个文件的C扩展. 解决方案¶ 要将一个文件转换为一个整型的文件描述符,使用 PyFile ...

  8. 【Luogu1393】动态逆序对(CDQ分治)

    [Luogu1393]动态逆序对(CDQ分治) 题面 题目描述 对于给定的一段正整数序列,我们定义它的逆序对的个数为序列中ai>aj且i < j的有序对(i,j)的个数.你需要计算出一个序 ...

  9. 【THUWC 2017】在美妙的数学王国中畅游

    数学王国里有n座城市,每座城市有三个参数\(f\),\(a\),\(b\),一个智商为\(x\)的人经过一座城市的获益\(f(x)\)是 若\(f=1\),则\(f(x)=\sin(ax+b)\): ...

  10. 关于hibernate中hql语句 case when的写法

    java hql case when的用法 if(null == sorter){ hql.append(" order by m.mDate desc,case when m.mealTi ...