1.根目录下的/Application/Home/Controller文件夹中的UserController.class.php文件,书写代码为:
~~~
<?php
<?php
namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;
class UserController extends Controller
{
private $appid="wx165112bf167af76c";
private $appSecret="c65b22bfcf03fdd98504eac299701b03";
private $WechatAuth="";//初始化WechatAuth类
private $access_token="";//缓存token
public function __construct(){
parent::__construct();//可能内部已经有这个构造方法了,因此加上这个
if(!session('token')){
$this->WechatAuth=new WechatAuth($this->appid,$this->appSecret);//初始化WechatAuth类
$WechatAuth=$this->WechatAuth;
$token=$WechatAuth->getAccessToken();
session(array('expire'=>$token['expires_in']));//设置过期时间
session('token',$token['access_token']);//缓存token
$this->access_token=$token;
}else{
$token=session('token');
$this->WechatAuth=new WechatAuth($this->appid,$this->appSecret,$token);//初始化WechatAuth类
$this->access_token=$token;//缓存token
}
}
//获取所有用户组
public function selectGroupsGet(){
header('Content-type:text/html;charset=utf-8;');
$WechatAuth=$this->WechatAuth;
$data=$WechatAuth->groupsGet();
var_dump($data);
}
}
~~~
2.注意内容:
![](https://box.kancloud.cn/143390e7c8601f611d84f13f325a395a_526x155.png)
3.在微信web开发者工具上测试结果为
![](https://box.kancloud.cn/2b02c325d80d0817b95ec0a9d18100b3_978x701.png)