2.根目录下的/Application/Home/Controller文件夹中的MassController.class.php文件,书写代码为:
~~~
<?php
namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;
class MassController extends Controller
{
private $appid="wx165112bf167af76c";
private $appSecret="c65b22bfcf03fdd98504eac299701b03";
private $WechatAuth="";//初始化WechatAuth类
private $access_token="";//缓存token
/**
* 微信api根路径
* @var string
*/
private $apiURL = 'https://api.weixin.qq.com/cgi-bin';
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 send(){
header('Content-type:text/html;charset=utf-8;');
$WechatAuth=$this->WechatAuth;
$msgtype="text";
$openid="orVDLwCI0NcQk0skZPNrfZkaj-aE";//暂时直接从数据库中复制的openid
$content="你好,我在预览群发消息";
$arr=$WechatAuth->meassageMass($msgtype,$openid,$content);
var_dump($arr);
}
/**
* 群发接口
*/
public function sendAllmessage(){
$group_id=null;
$msgtype="text";
$content="xi wang ke yi cheng gong a";
$data=array(
'filter'=>array("is_to_all"=>true,'group_id'=>$group_id),
$msgtype=>array('content'=>$content),
'msgtype'=>$msgtype
);
$arr=$this->api("message/mass/sendall",$data);
var_dump($arr);
}
/**
* 调用微信api获取响应数据
* @param string $name API名称
* @param string $data POST请求数据
* @param string $method 请求方式
* @param string $param GET请求参数
* @return array api返回结果
*/
public function api($name, $data = '', $method = 'POST', $param = '', $json = true){
$params = array('access_token' => $this->access_token);
if(!empty($param) && is_array($param)){
$params = array_merge($params, $param);
}
$url = "{$this->apiURL}/{$name}";
$data = json_encode($data);
$data = self::http($url, $params, $data, $method);
return json_decode($data, true);
}
/**
* 发送HTTP请求方法,目前只支持CURL发送请求
* @param string $url 请求URL
* @param array $param GET参数数组
* @param array $data POST的数据,GET请求时该参数无效
* @param string $method 请求方法GET/POST
* @return array 响应数据
*/
protected static function http($url, $param, $data = '', $method = 'GET'){
$opts = array(
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
);
/* 根据请求类型设置特定参数 */
$opts[CURLOPT_URL] = $url . '?' . http_build_query($param);
if(strtoupper($method) == 'POST'){
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $data;
if(is_string($data)){ //发送JSON数据
$opts[CURLOPT_HTTPHEADER] = array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data),
);
}
}
/* 初始化并执行curl请求 */
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
//发生错误,抛出异常
if($error) throw new \Exception('请求发生错误:' . $error);
return $data;
}
}
~~~
2.运行结果
![](https://box.kancloud.cn/5496c3b6f744cd25f6c8b44bb270413d_401x37.png)