WeChat public platform development - group messaging
Feb 20, 2017 pm 02:44 PMThis 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 |
參數(shù) | 是否必須 | 說明 |
---|---|---|
filter | 是 | 用于設定圖文消息的接收者 |
is_to_all | 否 | 用于設定是否向全部用戶發(fā)送,值為true或false,選擇true該消息群發(fā)給所有用戶,選擇false可根據(jù)group_id發(fā)送給指定群組的用戶 |
group_id | 否 | 群發(fā)到的分組的group_id,參加用戶管理中用戶分組接口,若is_to_all值為true,可不填寫group_id |
mpnews | 是 | 用于設定即將發(fā)送的圖文消息 |
media_id | 是 | 用于群發(fā)的消息的media_id |
msgtype | 是 | 群發(fā)的消息類型,圖文消息為mpnews,文本消息為text,語音為voice,音樂為music,圖片為image,視頻為video,卡券為wxcard |
title | 否 | 消息的標題 |
description | 否 | 消息的描述 |
thumb_media_id | 是 | 視頻縮略圖的媒體ID |
例子:發(fā)送給所有人
url:
https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=KBoNONaJZ4-KhafQVJoQ6VBX0F-bls7nAsJBn8Fy7GLwav4Be1lRJcob1RHH6wW35IxxFwkJnZfnc-On9EQITg3oxEWUw7O2YyVW9naDknu6PQX9fnSmQcr8ojTK8Ug-HDTcAAABXN
發(fā)送的json數(shù)據(jù):發(fā)送給所有人
{ "filter":{ "is_to_all":true }, "text":{ "content":"CONTENT" }, "msgtype":"text" }
返回數(shù)據(jù):
{ "errcode": 0, "errmsg": "send job submission success", "msg_id": 1000000003 }
參數(shù)意義:
參數(shù) | 說明 |
---|---|
type | 媒體文件類型,分別有圖片(image)、語音(voice)、視頻(video)和縮略圖(thumb),圖文消息為news |
errcode | 錯誤碼 |
errmsg | 錯誤信息 |
msg_id | 消息發(fā)送任務的ID |
msg_data_id | 消息的數(shù)據(jù)ID,該字段只有在群發(fā)圖文消息時,才會出現(xiàn)??梢杂糜谠趫D文分析數(shù)據(jù)接口中,獲取到對應的圖文消息的數(shù)據(jù),是圖文分析數(shù)據(jù)接口中的msgid字段中的前半部分,詳見圖文分析數(shù)據(jù)接口中的msgid字段的介紹。 |
錯誤碼及其以及查詢:
使用postman模擬https請求發(fā)送如下圖所示:
2)根據(jù)OpenID列表群發(fā)【訂閱號不可用,服務號認證后可用】
發(fā)送的http請求url:(注意:和上面的不同)
http請求方式: POST
https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN
數(shù)據(jù)格式:
{ "touser":[ "OPENID1", "OPENID2" ], "msgtype": "text", "text": { "content": "hello from boxer."} }
其中 OPENID1和OPENID2是我們要發(fā)送的微信用戶openId(用戶的唯一標示)。
例子:
發(fā)送"oF3PcsnsrMiJzEwalZZbAfWQpxCI","oF3PcshH1CUIhR_WYau6swUiPzlw" 兩個用戶。
內(nèi)容為:hello from boxer.歡迎來到百度
url:
https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=wRyTbnsiu18ssEhMPLf4bDfeT-Bt6e6tgR4CQGVLBipRcyJPkdAKPYfM6-qkKuHUN8uRKJh6Xvm0OuAdFgqOo8Ru8hoDxl-cGc9bh-ezJb2ZUcJSnQk2s416zI8kbEOfOGYdAFARJB
json數(shù)據(jù):
{ "touser":[ "oF3PcsnsrMiJzEwalZZbAfWQpxCI", "oF3PcshH1CUIhR_WYau6swUiPzlw" ], "msgtype": "text", "text": { "content": "hello from boxer.<a href=‘http://ipnx.cn/‘>歡迎希沃學院</a>"} }
返回數(shù)據(jù):
{ "errcode": 0, "errmsg": "send job submission success", "msg_id": 3147483654 }
使用postman模擬發(fā)送請求如下:
微信號接收到的內(nèi)容:
更多WeChat public platform development - group messaging相關文章請關注PHP中文網(wǎng)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
