微信公众号开发(九)群发消息接口
订阅号每日可以群发一条,服务号每个自然月可以群发4条。
1、根据标签进行群发【订阅号与服务号认证后均可用】
接口:https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN
群发文本
sendall_text.php
<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$data = '{
"filter":{
"is_to_all":false,
"tag_id":100
},
"text":{
"content":"CONTENT"
},
"msgtype":"text"
}';
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?"
."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;
返回:
{
"errcode": 0,
"errmsg": "send job submission success",
"msg_id": 1000000001
}
推送给index.php的XML文件
<xml>
<ToUserName>
<![CDATA[gh_6b9aa8a6f1e2]]>
</ToUserName>
<FromUserName>
<![CDATA[o4WmZ0qcNsoRiDW9LEi1X1gWVBZ0]]>
</FromUserName>
<CreateTime>1505397941</CreateTime>
<MsgType>
<![CDATA[event]]>
</MsgType>
<Event>
<![CDATA[MASSSENDJOBFINISH]]>
</Event>
<MsgID>1000000001</MsgID>
<Status>
<![CDATA[send success]]>
</Status>
<TotalCount>1</TotalCount>
<FilterCount>1</FilterCount>
<SentCount>1</SentCount>
<ErrorCount>0</ErrorCount>
<CopyrightCheckResult>
<Count>0</Count>
<ResultList></ResultList>
<CheckState>0</CheckState>
</CopyrightCheckResult>
</xml>
发送语音
发送语音和发送文本类似,不过格式为:
{
"filter":{
"is_to_all":false,
"tag_id":2
},
"voice":{
"media_id":"123dsdajkasd231jhksad"
},
"msgtype":"voice"
}
发送图文
sendall_mpnews.php
<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$data = '{
"filter":{
"is_to_all":false,
"tag_id":100
},
"mpnews":{
"media_id":"FrsRJ3g3BHR-pIkuFLARnMOwMvwukEiXaYvy1xmpoX0"
},
"msgtype":"mpnews",
"send_ignore_reprint":0
}';
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?"
."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;
返回:
{"errcode":48008,"errmsg":"no permission for this msgtype hint: [PgBA9a0938ge25]"}
说是没有权限,可以调用预览接口测试:
<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$data = '{
"touser":"o4WmZ0h-4huBUVQUczx2ezaxIL9c",
"mpnews":{
"media_id":"FrsRJ3g3BHR-pIkuFLARnMOwMvwukEiXaYvy1xmpoX0"
},
"msgtype":"mpnews"
}';
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?"
."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;
发送图片
<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$data = '{
"filter":{
"is_to_all":false,
"tag_id":100
},
"image":{
"media_id":"FrsRJ3g3BHR-pIkuFLARnGMeH3WkYJCu0ZPZ_OqQOB8"
},
"msgtype":"image"
}';
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?"
."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;
发送视频:
此处视频的media_id需通过POST请求到下述接口特别地得到:https://api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=ACCESS_TOKEN POST得到,
uploadvideo.php
<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$data = '{
"media_id": "aTL-93EXcL4F9g4SzTdBuokPQgS_qXisgswHO02iCrqbVpU_gL_tanb9LXZ2Lc2r",
"title": "群发视频",
"description": "你好吗?"
}';
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo?"
."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;
返回结果:
{
"type": "video",
"media_id": "4KExkgRWTofVOgQZRkCtpTEyFhuYk1Xwr1y-biXNS93U7hICK1rtHgXs8uzntW60",
"created_at": 1505400804
}
sendall_video.php
<?php
@header('Content-type: text/plain;charset=UTF-8');
require_once("../Utils.php");
$data = '{
"filter":{
"is_to_all":false,
"tag_id":100
},
"mpvideo":{
"media_id":"4KExkgRWTofVOgQZRkCtpTEyFhuYk1Xwr1y-biXNS93U7hICK1rtHgXs8uzntW60"
},
"msgtype":"mpvideo"
}';
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?"
."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;
2、根据OpenID列表群发【订阅号不可用,服务号认证后可用】
接口:https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN
消息格式类似:
{
"touser":[
"OPENID1",
"OPENID2"
],
"mpnews":{
"media_id":"123dsdajkasd231jhksad"
},
"msgtype":"mpnews",
"send_ignore_reprint":0
}
这里不再列出代码。
3、删除群发消息【订阅号与服务号认证后均可用】
接口:https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token=ACCESS_TOKEN
post数据:
{
"msg_id":30124,
"article_idx":2
}
返回:
{
"errcode":0,
"errmsg":"ok"
}
说明:
1、只有已经发送成功的消息才能删除
2、删除消息是将消息的图文详情页失效,已经收到的用户,还是能在其本地看到消息卡片。
3、删除群发消息只能删除图文消息和视频消息,其他类型的消息一经发送,无法删除。
4、如果多次群发发送的是一个图文消息,那么删除其中一次群发,就会删除掉这个图文消息也,导致所有群发都失效
4、查询群发消息发送状态【订阅号与服务号认证后均可用】
接口:https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token=ACCESS_TOKEN
post数据:
{
"msg_id": "201053012"
}
返回:
{
"msg_id":201053012,
"msg_status":"SEND_SUCCESS"
}
相关博客