欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php实现微信公众号无限群发

程序员文章站 2022-10-11 12:07:03
利用微信客服接口进行各类消息的无限群发 sendallmsg.php 

利用微信客服接口进行各类消息的无限群发

sendallmsg.php

<?php
  /*
    author:yf
    使用说明:微信公众号无限群发接口,使用实例:   
    $test = new sendallmsg("你的appid","你的appsecret");
    $test->sendmsgtoall(); //调用群发方法
    注:1.使用条件:认证号或测试号
      2.群发消息内容可为图文、文本、音乐等,$data具体内容参照微信开发文档/客服接口
      3.若用户量过万,需修改getuserinfo(),具体参照信开发文档/获取关注者列表
       
    新手上路,大神们多多指点,谢谢
  */
  interface isendallmsg{
    function getdata($url); //curl 发送get请求
    function postdata($url,$data); //curl 发送post请求
    function getaccesstoken();  //在构造方法中已调用该方法来获取access_token,注意它在wx服务器的保存时间7200s
    function sendmsgtoall(); //群发消息方法,发送的消息$data 可自行修改
  }
  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":"测试一下,抱歉打扰各位"
              }
            }';
        $this->postdata($url,$data);
      }
    }
  }
  $test = new sendallmsg("yourappid","yourappsecret");
  $test->sendmsgtoall();
   
?>

以上就是本文的全部内容了,希望大家能够喜欢。