1.首先在Vendor下新建SendAllMsg文件夾,然后將SendAllMsg.php放入
SendAllMsg.php
<?php
/*
+----------------------------------------------------------------------
+ Title : 微信公眾號無線群發接口
+ Author : wjwer
+ Version : 1.0
+ Initial-Time : 2017.3.20
+ Last-time : 2017.3.20 wjwer
+ Desc : 使用說明:微信公眾號無線群發接口,使用實例:
$test = new \SendAllMsg(C('WX_APPID'),C('WX_CRYPT'));
$test->sendMsgToAll(); //調用群發方法
注:1.使用條件:認證號或測試號
2.群發消息內容可為圖文、文本、音樂等,$data具體內容參照微信開發文檔/客服接口
3.若用戶量過萬,需修改getUserInfo(),具體參照信開發文檔/獲取關注者列表
+Ps : 參考文檔 https://mp.weixin.qq.com/wiki/15/40b6865b893947b764e2de8e4a1fb55f.html
+----------------------------------------------------------------------
*/
interface iSendAllMsg{
function getData($url); //curl 發送get請求
function postData($url,$data); //curl 發送post請求
function getAccessToken(); //在構造方法中已調用該方法來獲取access_token,注 意它在wx服務器的保存時間7200s
function sendMsgToAll(); //群發消息方法,發送的消息$data 可自行修改
}
class SendAllMsg implements iSendAllMsg{
private $appId;
private $appSecret;
private $access_token;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
$this->access_token = $this->getAccessToken();
}
function getData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function postData($url,$data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
function getAccessToken(){
$url = "https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;
$res = $this->getData($url);
$jres = json_decode($res,true);
$access_token = $jres['access_token'];
return $access_token;
}
private function getUserInfo(){
$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token;
$res = $this->getData($url);
$jres = json_decode($res,true);
//print_r($jres);
$userInfoList = $jres['data']['openid'];
return $userInfoList;
}
function sendMsgToAll(){
$userInfoList = $this->getUserInfo();
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send? access_token=".$this->access_token;
foreach($userInfoList as $val){
$data = '{
"touser":"'.$val.'",
"msgtype":"text",
"text":
{
"content":"再來一次"
}
}';
$this->postData($url,$data);
}
}
}
?>
在控制器中調用:
<?php
namespace Admin\Controller;
use Think\Controller;
header('Content-Type:text/html; charset= utf-8');
class CeshiController extends PublicController{
public function index(){
//微信群發
Vendor('SendAllMsg.SendAllMsg');
$test = new \SendAllMsg(C('WX_APPID'),C('WX_CRYPT'));
$test->sendMsgToAll(); //調用群發方法
}
}
順帶把發送其他消息的也說一下吧
微信客服接口總摘取
各消息類型所需的JSON數據包如下:
發送文本消息
"touser":"OPENID",
"msgtype":"text",
"text":
{
"content":"Hello World"
}
}
發送圖片消息
{
"touser":"OPENID",
"msgtype":"image",
"image":
{
"media_id":"MEDIA_ID"
}
}
發送語音消息
{
"touser":"OPENID",
"msgtype":"voice",
"voice":
{
"media_id":"MEDIA_ID"
}
}
發送視頻消息
{
"touser":"OPENID",
"msgtype":"video",
"video":
{
"media_id":"MEDIA_ID",
"thumb_media_id":"MEDIA_ID",
"title":"TITLE",
"description":"DESCRIPTION"
}
}
發送音樂消息
{
"touser":"OPENID",
"msgtype":"music",
"music":
{
"title":"MUSIC_TITLE",
"description":"MUSIC_DESCRIPTION",
"musicurl":"MUSIC_URL",
"hqmusicurl":"HQ_MUSIC_URL",
"thumb_media_id":"THUMB_MEDIA_ID"
}
}
發送圖文消息(點擊跳轉到外鏈) 圖文消息條數限制在8條以內,注意,如果圖文數超過8,則將會無響應。
{
"touser":"OPENID",
"msgtype":"news",
"news":{
"articles": [
{
"title":"Happy Day",
"description":"Is Really A Happy Day",
"url":"URL",
"picurl":"PIC_URL"
},
{
"title":"Happy Day",
"description":"Is Really A Happy Day",
"url":"URL",
"picurl":"PIC_URL"
}
]
}
}
發送圖文消息(點擊跳轉到圖文消息頁面) 圖文消息條數限制在8條以內,注意,如果圖文數超過8,則將會無響應。
{
"touser":"OPENID",
"msgtype":"mpnews",
"mpnews":
{
"media_id":"MEDIA_ID"
}
}
發送卡券
{
"touser":"OPENID",
"msgtype":"wxcard",
"wxcard":{
"card_id":"123dsdajkasd231jhksad"
},
}