Php enables unlimited group sending of WeChat public accounts

Source: Internet
Author: User
This article will share with you the idea of using the customer service interface implemented by php for unlimited mass sending of various types of messages. if you need it, you can refer to the customer service interface for unlimited mass sending of all kinds of messages.

SendAllMsg. php

Signature <? Php/* Author: yf usage instructions: public account unlimited group interface, use instance: $ test = new SendAllMsg ("your appId", "your appSecret "); $ test-> sendMsgToAll (); // call the group sending method. note: 1. usage conditions: authentication number or test number 2. the group message content can be text, text, music, etc. for details about $ data, refer to the development documentation/customer service interface 3. if the number of users exceeds, you need to modify getUserInfo (). For details, refer to the letter development documentation/getting the list of followers, thank you */interface iSendAllMsg {function getData ($ url); // curl sends the get request function postData ($ url, $ data); // curl sends the post request function getAccessToken (); // This method has been called in the constructor to obtain the access_token. Note that it is saved on the wx server at 7200 s function sendMsgToAll (); // the group message sending method, the Sent Message $ data can be modified by yourself} class SendAllMsg implements iSendAllMsg {private $ appId; private $ appSecret; private $ access_token; // public function _ construct ($ appId, $ appSecret) {$ this-> appId = $ appId; $ this-> appSecret = $ appSecret; $ this-> access_token = $ this-> getAccessToken ();} // function getData ($ url) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) '); curl_setopt ($ ch, CURLOPT_ENCODING, 'gzip '); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); $ data = curl_exec ($ ch); curl_close ($ ch); return $ data ;} // function postData ($ url, $ data) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ ch, success, FALSE); curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) '); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ ch, CURLOPT_AUTOREFERER, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); $ tmpInfo = curl_exec ($ ch); if (curl_errno ($ ch) {return curl_error ($ ch );} curl_close ($ ch); return $ tmpInfo;} // function getAccessToken () {$ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= ". $ This-> appId. "& secret = ". $ this-> appSecret; $ res = $ this-> getData ($ url); $ jres = json_decode ($ res, true ); $ access_token = $ jres ['Access _ token']; return $ access_token;} // private function getUserInfo () {$ url =" https://api.weixin.qq.com/cgi-bin/user/get?access_token= ". $ This-> access_token; $ res = $ this-> getData ($ url); $ jres = json_decode ($ res, true); // print_r ($ jres ); $ userInfoList = $ jres ['data'] ['openid']; return $ userInfoList;} function sendMsgToAll () {$ userInfoList = $ this-> getUserInfo (); $ url =" https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token= ". $ This-> access_token; foreach ($ userInfoList as $ val) {$ data = '{"touser ":"'. $ val. '"," msgtype ":" text "," text ": {" content ":" test, sorry to disturb you "}}'; $ this-> postData ($ url, $ data) ;}}$ test = new SendAllMsg ("YOURappId", "YOURappSecret "); $ test-> sendMsgToall ();?>

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.