微信公眾號開發(九)群發消息接口
訂閱號每日可以群發一條,服務號每個自然月可以群發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、如果多次群發發送的是一個圖文消息,那么刪除其中一次群發,就會刪除掉這個圖文消息也,導致所有群發都失效
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"
}
