Utilize WeChat customer service interface for unlimited mass sending of various messages
- ? /*
- Author:yf
- Instructions for use: WeChat public account wireless group sending interface, usage example:
- $test = new SendAllMsg("your appId", "your appSecret");
- $test->sendMsgToAll(); //Call the mass sending method
- Note: 1. Conditions of use: certification number or test number
- 2. The content of the mass message can be graphics, text, music, etc. For the specific content of $data, please refer to WeChat Development Documents/Customer Service Interface
- 3. If the number of users exceeds 10,000, you need to modify getUserInfo(). For details, please refer to the development documents/get follower list
-
- Newbies are starting to get started, please give your advice, thank you
- */
- interface iSendAllMsg{
- function getData($url); //curl sends a get request
- function postData($url,$data); //curl sends a post request
- function getAccessToken(); //This method has been called in the construction method to obtain access_token, please note Its storage time on the wx server is 7200s
- function sendMsgToAll(); //Group messaging method, the message $data sent 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, CURLOPT_SSL_VERIFYHOST , 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 it, sorry to bother you"
- }
- }';
- $this ->postData($url,$data);
- }
- }
- }
- $test = new SendAllMsg("YOURappId","YOURappSecret");
- $test->sendMsgToall();
-
- ?>
Copy code
|