PHP微信公众平台oauth2.0网页授权登录类的封装demo

时间:2022-01-26 10:21:06

一、微信授权使用的是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