Home >WeChat Applet >WeChat Development >WeChat public platform development - group messaging
This article mainly introduces the development of mass messaging on the WeChat public platform. Detailed information is compiled here to explain the process of mass messaging on the WeChat public platform. Friends in need can refer to the following
1. Purpose
Finished sending group messages in the WeChat official account. Here just complete the simple text sending. You can also send voice pictures, etc., but the data format is different. There is a link below to query the data sending format of the data type.
2. The process of sending group text messages
Get a test public account (those who have an account do not need a test account, but formal accounts have more restrictions )
Users follow the public account above
Get our access_token through appid and appsecret
Group text messages through access_token
3. Get a test public account + follow the public account
1), get a public test account
Visit the above link and select "Interface Test Number Application" to directly open http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index through WeChat client Scan the QR code to log in.
After logging in, you can get the information of a test public account. There are mainly two parameters, appId and appsecret, which will uniquely identify an official account, and they need to be used as parameters to obtain the user's information. ,
2) Configuring interface information
You can refer to the WeChat access instructions for this step. This page provides an example download of php, which is very simple and basically Just modify the custom TOKEN, and then put the verification page on your own server.
Here I provide an example of what I did:
Prepare resources:
Domain name + space (mine is sae space + Wanwang domain name), PHP for verification only File
I created a wx_sample.php
wx_sample.php
<?php /** * wechat php test */ //define your token define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->valid(); class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!empty( $keyword )) { $msgType = "text"; $contentStr = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception(‘TOKEN is not defined!‘); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
in the space root directory pointed to by the domain name and then filled in Configuration information Token (must be consistent with the token in wx_sample.php above), URL (the address of wx_sample.php)
Then submit it
If the prompt fails, please check the Token and URL [If it is your own domain name and space, please register; Baidu sae and Sina sae need to apply for it yourself and pass the certification (just take a photo of your hand holding the ID and upload it, it is very simple and the shortest It will take 2 days), this step is necessary】
3) Configure the JS interface security domain name
Be sure not to include a protocol when filling in this domain name, for example http://www.sagosoft. com/; This is wrong, this is a URL, not a domain name
The domain name should be similar to www.sagosoft.com [Otherwise, invalid url domain will be prompted when accessing WeChat js-sdk]
4) Follow the public account
Only when a user follows this public account can he authorize a third party to log in and obtain user information by opening a link with public account information. operation. Therefore, we also need to use our WeChat to follow the WeChat ID. The operation is as follows:
It is still the page that jumped after successful login. We can see that the page has a QR code. We can scan the Follow the QR code. If you follow successfully, there will be one more user's information in the "User List" on the right. As shown in the figure below:
5) Configure the callback function
We access third-party web pages (that is, our own web pages) on the WeChat client When using WeChat, we can use the WeChat web page authorization mechanism. We not only need the appid and appsecret obtained previously, but also need to have the domain name settings for the callback after the user authorizes, that is, where the page will jump to after the user authorizes. The specific configuration is as follows:
Still on the page just now, there is a "Web page authorization to obtain basic user information", click on the subsequent modification
Fill in the callback domain name:
The domain name is the root domain name configured above. If the URL you filled in in the "Interface Configuration Information" above is zcr.sinaaappc.com/wx_sample.php, just fill in zcr.sinaaappc here. .com will do.
If your URL has not been blacklisted,
will appear at the top
Note:
1. What is filled in here is the domain name (a string), not the URL, so please do not add http:// and other protocol headers;
2. Authorization The callback domain name configuration specification is the full domain name. For example, the domain name that requires web page authorization is: www.qq.com. After configuration, the pages under this domain name are http://www.qq.com/music.html and http://www.qq .com/login.html can perform OAuth2.0 authentication. But ttp://pay.qq.com, http://music.qq.com, http://qq.com cannot perform OAuth2.0 authentication
At this point, we are done After obtaining and configuring the public account test account, users have already followed the WeChat public account.
4. Obtain our access_token through appid and appsecret
Access_token is the globally unique ticket of the official account. The official account needs to use access_token when calling each interface. Developers need to store it properly. At least 512 characters of space must be reserved for access_token storage. The validity period of access_token is currently 2 hours and needs to be refreshed regularly. Repeated acquisition will cause the last access_token to become invalid.
Getting method:
http request method: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
Parameter Description
Is it necessary | Description | |
---|---|---|
Yes | Get access_token fill in client_credential | |
Yes | Third-party user’s unique credential | |
is the | third-party user’s unique credential key, that is, appsecret |
Description | ||
---|---|---|
Obtained voucher | ||
Voucher validity time, unit: seconds |
参数 | 是否必须 | 说明 |
---|---|---|
filter | 是 | 用于设定图文消息的接收者 |
is_to_all | 否 | 用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,选择false可根据group_id发送给指定群组的用户 |
group_id | 否 | 群发到的分组的group_id,参加用户管理中用户分组接口,若is_to_all值为true,可不填写group_id |
mpnews | 是 | 用于设定即将发送的图文消息 |
media_id | 是 | 用于群发的消息的media_id |
msgtype | 是 | 群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video,卡券为wxcard |
title | 否 | 消息的标题 |
description | 否 | 消息的描述 |
thumb_media_id | 是 | 视频缩略图的媒体ID |
例子:发送给所有人
url:
https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=KBoNONaJZ4-KhafQVJoQ6VBX0F-bls7nAsJBn8Fy7GLwav4Be1lRJcob1RHH6wW35IxxFwkJnZfnc-On9EQITg3oxEWUw7O2YyVW9naDknu6PQX9fnSmQcr8ojTK8Ug-HDTcAAABXN
发送的json数据:发送给所有人
{ "filter":{ "is_to_all":true }, "text":{ "content":"CONTENT" }, "msgtype":"text" }
返回数据:
{ "errcode": 0, "errmsg": "send job submission success", "msg_id": 1000000003 }
参数意义:
参数 | 说明 |
---|---|
type | 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb),图文消息为news |
errcode | 错误码 |
errmsg | 错误信息 |
msg_id | 消息发送任务的ID |
msg_data_id | 消息的数据ID,该字段只有在群发图文消息时,才会出现。可以用于在图文分析数据接口中,获取到对应的图文消息的数据,是图文分析数据接口中的msgid字段中的前半部分,详见图文分析数据接口中的msgid字段的介绍。 |
错误码及其以及查询:
使用postman模拟https请求发送如下图所示:
2)根据OpenID列表群发【订阅号不可用,服务号认证后可用】
发送的http请求url:(注意:和上面的不同)
http请求方式: POST
https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN
数据格式:
{ "touser":[ "OPENID1", "OPENID2" ], "msgtype": "text", "text": { "content": "hello from boxer."} }
其中 OPENID1和OPENID2是我们要发送的微信用户openId(用户的唯一标示)。
例子:
发送"oF3PcsnsrMiJzEwalZZbAfWQpxCI","oF3PcshH1CUIhR_WYau6swUiPzlw" 两个用户。
内容为:hello from boxer.欢迎来到百度
url:
https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=wRyTbnsiu18ssEhMPLf4bDfeT-Bt6e6tgR4CQGVLBipRcyJPkdAKPYfM6-qkKuHUN8uRKJh6Xvm0OuAdFgqOo8Ru8hoDxl-cGc9bh-ezJb2ZUcJSnQk2s416zI8kbEOfOGYdAFARJB
json数据:
{ "touser":[ "oF3PcsnsrMiJzEwalZZbAfWQpxCI", "oF3PcshH1CUIhR_WYau6swUiPzlw" ], "msgtype": "text", "text": { "content": "hello from boxer.<a href=‘http://www.php.cn/‘>欢迎希沃学院</a>"} }
返回数据:
{ "errcode": 0, "errmsg": "send job submission success", "msg_id": 3147483654 }
使用postman模拟发送请求如下:
微信号接收到的内容:
更多WeChat public platform development - group messaging相关文章请关注PHP中文网!