亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home WeChat Applet WeChat Development WeChat public platform development - group messaging

WeChat public platform development - group messaging

Feb 20, 2017 pm 02:44 PM
Micro-channel public platform

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

  1. Get a test public account (those who have an account do not need a test account, but formal accounts have more restrictions )

  2. Users follow the public account above

  3. Get our access_token through appid and appsecret

  4. 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. ,

WeChat public platform development - group messaging

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)

WeChat public platform development - group messaging

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]

WeChat public platform development - group messaging

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:

WeChat public platform development - group messaging

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

WeChat public platform development - group messaging

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.

WeChat public platform development - group messaging

If your URL has not been blacklisted,

WeChat public platform development - group messaging

WeChat public platform development - group messaging 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


##ParameterIs it necessaryDescriptiongrant_typeYesGet access_token fill in client_credentialappidYesThird-party user’s unique credentialsecret is the third-party user’s unique credential key, that is, appsecret
Return instructions

Under normal circumstances, WeChat will return the following JSON data packet to the public account:

{"access_token":"ACCESS_TOKEN","expires_in" :7200}


ParameterDescriptionaccess_tokenObtained voucherexpires_inVoucher validity time, unit: seconds##

錯誤時微信會返回錯誤碼等信息,JSON數(shù)據(jù)包示例如下(該示例為AppID無效錯誤):

{"errcode":40013,"errmsg":"invalid appid"}

例子:

獲取access_token:

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx41cb8dbd827a16e9&secret=d4624c36b6795d1d99dcf0547af5443d

返回數(shù)據(jù):

{
 "access_token": "qR5UK2vMf5aTHV8e-uB10FZW0caTZm_1kbkUe4OPK2ILVvNaoa7pLzYWqLUAmx6Sjq1E7pKHrVAtuG0_1MPkqmDfOkm2750kaLWNk59DS-iDOpjjxompJtXa3WhbN5FKRWNhADAVAR",
 "expires_in": 7200
}

5、通過access_token群發(fā)短信

  在公眾平臺網(wǎng)站上,為訂閱號提供了每天一條的群發(fā)權限,為服務號提供每月(自然月)4條的群發(fā)權限。而對于某些具備開發(fā)能力的公眾號運營者,可以通過高級群發(fā)接口,實現(xiàn)更靈活的群發(fā)能力。

請注意:

1、對于認證訂閱號,群發(fā)接口每天可成功調(diào)用1次,此次群發(fā)可選擇發(fā)送給全部用戶或某個分組;
2、對于認證服務號雖然開發(fā)者使用高級群發(fā)接口的每日調(diào)用限制為100次,但是用戶每月只能接收4條,無論在公眾平臺網(wǎng)站上,還是使用接口群發(fā),用戶每月只能接收4條群發(fā)消息,多于4條的群發(fā)將對該用戶發(fā)送失??;
3、具備微信支付權限的公眾號,在使用群發(fā)接口上傳、群發(fā)圖文消息類型時,可使用標簽加入外鏈;
4、開發(fā)者可以使用預覽接口校對消息樣式和排版,通過預覽接口可發(fā)送編輯好的消息給指定用戶校驗效果。

1)根據(jù)分組進行群發(fā)【訂閱號與服務號認證后均可用】

調(diào)用接口:

http請求方式: POST
https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN 在body添加如下數(shù)據(jù)(以JSON格式數(shù)據(jù)發(fā)送)——發(fā)送其他格式數(shù)據(jù),只需要改里面參數(shù)信息即可,具體可查看微信官方文檔:

{
 "filter":{
  "is_to_all":false,
  "group_id":2
 },
 "text":{
  "content":"CONTENT"
 },
 "msgtype":"text"
}

參數(shù)說明:


參數(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ā)送如下圖所示:

WeChat public platform development - group messaging

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ā)送請求如下:

WeChat public platform development - group messaging

微信號接收到的內(nèi)容:

WeChat public platform development - group messaging


更多WeChat public platform development - group messaging相關文章請關注PHP中文網(wǎng)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)