Mass message for php public platform development and mass message for the public
1. Purpose
Send messages to the public account. Here is just to complete simple text sending. You can also send voice images, but the data sending format is different. There is a link below to query the data sending format.
2. Process of sending mass messages
Obtain the public account for testing (if you have an account, you do not need to test the account, but there are many official account restrictions). Follow the public account above to get our access_token through appid and appsecret and send a group of text messages through access_token.
3. Obtain the test public account + follow public account
1) obtain the public test account
Access the connection above, select "Interface Test number application" to get to open the http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo directly? Action = showinfo & t = sandbox/index: scan the client to log on.
After logon, you can obtain information about a public account. There are two parameters: appId and appsecret, which uniquely identify a public number and use them as parameters to obtain user information. ,
2) configure the Interface Information
For this step, you can refer to the access instructions. This page provides a php instance for downloading. It is easy to modify the custom TOKEN and then place the verification page on your server.
Here I provide an example:
Prepare resources:
Domain name + space (My sae space + hichina domain name), PHP file for verification only
I created a wx_sample.php
Wx_sample.php
<?php/*** wechat php test*///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();$wechatObj->valid();class wechatCallbackapiTest{public function valid(){$echoStr = $_GET["echostr"];//valid signature , optionif($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 dataif (!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 yourselfif (!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 rulesort($tmpArr, SORT_STRING);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){return true;}else{return false;}}}?>
Then fill in the configuration information Token (must be consistent with the token in wx_sample.php above), URL (wx_sample.php address)
Then you can submit it.
If the message fails, check the Token and URL. [if your domain name and space are used, apply for an ICP filing; baidu sae and Sina sae must apply for and pass the certification by themselves (that is, they must take a hand-held Certificate photo and upload it in at least two days). This step is required]
3) configure the JS interface Security Domain Name
Enter this domain name is not to bring protocol, such as http://www.sagosoft.com/This is wrong, this is the URL is not a Domain Name
The domain name should be similar to www.sagosoft.com [otherwise, the invalid url domain will be prompted during js-sdk access]
4) follow the public account
Only when a user pays attention to this public account can he authorize a third party to log on and obtain user information through the link with the public account information. Therefore, we need to use our attention number as follows:
We can see that there is a QR code on this page, and we can scan the QR code to follow it, follow the "user list" on the right to add more user information. As shown in:
5) configure the callback function
When the client accesses a third-party webpage (that is, our own webpage), we can use the webpage authorization mechanism, we need not only the previously obtained appid and appsecret, but also the domain name settings for callback after user authorization, that is, where the page will jump to after user authorization. The specific configuration is as follows:
On the previous page, there is a "webpage authorization to obtain basic user information" and click Modify next to it.
Enter the callback Domain Name:
The domain name is the root domain name configured above. If you fill in zcr.sinaaappc.com/wx_sample.php in the "interface configuration information" above, you only need to fill in zcr.sinaaappc.com.
If your website is not blacklisted, it will appear on the top
Note:
1. Enter the domain name (a string) instead of the URL. Therefore, do not add http: // and other protocol headers;
2, authorization callback domain name configuration specification for the full domain name, such as the need for web page authorization Domain Name: www.qq.com, after the configuration of the domain name under the page http://www.qq.com/music.html, http://www.qq.com/login.html can be OAuth2.0 authentication. But http://pay.qq.com, http://music.qq.com, http://qq.com cannot perform OAuth2.0 Authentication
Here, we have completed the acquisition and configuration of the Public Account test account, and the user has followed the public account.
4. Get our access_token through appid and appsecret
Access_token is the globally unique ticket of the public account. access_token is required when the Public Account calls each interface. Developers need to properly store them. The storage of access_token must contain at least 512 characters. The validity period of access_token is currently 2 hours. You need to refresh it regularly. Repeated access_token acquisition will invalidate the last access_token.
Method:
Http Request Method: GET
Https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = APPID & secret = APPSECRET
Parameter description:
Return description
Normally, the following JSON data packet is returned to the Public Account:
Copy codeCode: {"access_token": "ACCESS_TOKEN", "expires_in": 7200}
When an error occurs, an error code or other information is returned. An example of a JSON data packet is as follows (this example is an invalid AppID error ):
Copy codeCode: {"errcode": 40013, "errmsg": "invalid appid "}
Example:
Get access_token:
Copy codeThe Code is as follows: https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = wx4d1cb8dbd827a16e9 & secret = d462d4c36b116795d1d99dcf0547af5443d
Returned data:
{"access_token": "qR5UK2vMf5aTHV8e-uB10FZW0caTZm_1kbkUe4OPK2ILVvNaoa7pLzYWqLUAmx6Sjq1E7pKHrVAtuG0_1MPkqmDfOkm2750kaLWNk59DS-iDOpjjxompJtXa3WhbN5FKRWNhADAVAR","expires_in": 7200}
5. send SMS messages through access_token
On the website of the public platform, the Group Sending permission is provided for the subscription number one per day, and the Group Sending permission is provided for the service number four per month (calendar month. For some public account operators with development capabilities, they can use the advanced group sending interface to achieve more flexible Group Sending capabilities.
Note:
1. for authentication subscription numbers, the Group Sending interface can be successfully called once a day. This group can be sent to all users or a group;
2. For the certification service number, although developers can use the advanced group-sending interface for up to 100 calls per day, users can only receive four messages per month, whether on the public platform website or using the interface group, A user can receive only four group messages each month, and more than four group messages will fail to be sent to the user;
3. If you have a public account with the payment permission, you can use the <a> label to add the public account to the external link when using the group-sending interface to upload or send group text messages;
4. developers can use the preview interface to verify the message style and layout. The preview interface can send edited messages to the specified user for verification.
1) group by group. [Both the subscription number and service number can be used after authentication]
Call interface:
Copy codeThe Code is as follows: http Request Method: POSThttps: // api.weixin.qq.com/cgi-bin/message/mass/sendall? Access_token = ACCESS_TOKEN
Add the following data to the body (send data in JSON format) -- send data in other formats. You only need to modify the parameter information. For details, refer to the official documentation:
{"filter":{"is_to_all":false,"group_id":2},"text":{"content":"CONTENT"},"msgtype":"text"}
Parameter description:
Example: Send to everyone
Url:
Copy codeThe Code is as follows: https://api.weixin.qq.com/cgi-bin/message/mass/sendall? Access_token = KBoNONaJZ4-KhafQVJoQ6VBX0F-bls7nAsJBn8Fy7GLwav4Be1lRJcob1RHH6wW35IxxFwkJnZfnc-On9EQITg3oxEWUw7O2YyVW9naDknu6PQX9fnSmQcr8ojTK8Ug-HDTcAAABXN
Json data sent: send to all users
{"filter":{"is_to_all":true},"text":{"content":"CONTENT"},"msgtype":"text"}
Returned data:
{"errcode": 0,"errmsg": "send job submission success","msg_id": 1000000003}
Parameter meaning:
Error Codes and query:
Global error code parsing
Use postman to simulate https request sending, as shown in:
2) send a group of [subscription numbers unavailable, available after service number authentication] based on the OpenID list]
Http request url: (Note: different from the above)
Copy codeThe Code is as follows: http Request Method: POSThttps: // api.weixin.qq.com/cgi-bin/message/mass/send? Access_token = ACCESS_TOKEN
Data format:
Copy codeThe Code is as follows: {"touser": ["OPENID1", "OPENID2"], "msgtype": "text", "text": {"content": "hello from boxer. "}}
OPENID1 and OPENID2 are the user openId (Unique User ID) to be sent ).
Example:
Send "oF3PcsnsrMiJzEwalZZbAfWQpxCI ","OF3PcshH1CUIhR_WYau6swUiPzlw" two users.
Content: hello from boxer. <a href = 'www .baidu.com '> welcome to Baidu </a>
Url:
Copy codeThe Code is as follows: https://api.weixin.qq.com/cgi-bin/message/mass/send? Access_token = wRyTbnsiu18ssEhMPLf4bDfeT-Bt6e6tgR4CQGVLBipRcyJPkdAKPYfM6-qkKuHUN8uRKJh6Xvm0OuAdFgqOo8Ru8hoDxl-cGc9bh-ezJb2ZUcJSnQk2s416zI8kbEOfOGYdAFARJB
Json data:
{"Touser": ["oF3PcsnsrMiJzEwalZZbAfWQpxCI", "audio"], "msgtype": "text", "text": {"content": "hello from boxer. <a href = 'HTTP: // www.seewoedu.com/'> welcome to xivo college </a> "}}
Returned data:
{"errcode": 0,"errmsg": "send job submission success","msg_id": 3147483654}
Use postman to send the following requests:
Received content:
Thank you!
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.