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


Posted in PHP onOctober 11, 2015

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

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();
   
?>

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

PHP 相关文章推荐
PHP 程序授权验证开发思路
Jul 09 PHP
php压缩多个CSS为一个css的代码并缓存
Apr 21 PHP
PHP 中检查或过滤IP地址的实现代码
Nov 27 PHP
让PHP更快的提供文件下载的代码
Jun 13 PHP
php写的带缓存数据功能的mysqli类
Sep 06 PHP
mcrypt启用 加密以及解密过程详细解析
Aug 07 PHP
php版微信公众平台之微信网页登陆授权示例
Sep 23 PHP
PHP lcfirst()函数定义与用法
Mar 08 PHP
php使用socket调用http和smtp协议实例小结
Jul 26 PHP
PHP调用接口API封装的例子
Oct 11 PHP
浅谈Laravel中的三种中间件的作用
Oct 13 PHP
Laravel自动生成UUID,从建表到使用详解
Oct 24 PHP
PHP+Mysql+jQuery中国地图区域数据统计实例讲解
Oct 10 #PHP
PHP+Mysql+jQuery文件下载次数统计实例讲解
Oct 10 #PHP
刷新PHP缓冲区为你的站点加速
Oct 10 #PHP
PHP和Mysql中转UTF8编码问题汇总
Oct 10 #PHP
[原创]ThinkPHP中SHOW_RUN_TIME不能正常显示运行时间的解决方法
Oct 10 #PHP
PHP内存使用情况如何获取
Oct 10 #PHP
PHP中Session和Cookie是如何操作的
Oct 10 #PHP
You might like
PHP文件下载类
2006/12/06 PHP
phpMyAdmin2.11.6安装配置方法
2008/08/24 PHP
php遍历文件夹下的所有文件和子文件夹示例
2014/03/20 PHP
php动态生成函数示例
2014/03/21 PHP
采用thinkphp自带方法生成静态html文件详解
2014/06/13 PHP
HTML中不支持静态Expando的元素的问题
2007/03/08 Javascript
javaScript 读取和设置文档元素的样式属性
2009/04/14 Javascript
细说javascript函数从函数的构成开始
2013/08/29 Javascript
JQuery 两种方法解决刚创建的元素遍历不到的问题
2016/04/13 Javascript
jquery pagination插件动态分页实例(Bootstrap分页)
2016/12/23 Javascript
jQuery取得元素标签名称小结(附代码)
2017/08/16 jQuery
JavaScript中运算符规则和隐式类型转换示例详解
2017/09/06 Javascript
Vuex提升学习篇
2018/01/11 Javascript
微信小程序实现YDUI的ScrollNav组件
2018/02/02 Javascript
vue项目使用微信公众号支付总结及遇到的坑
2018/10/23 Javascript
改变layer confirm弹窗按钮的颜色方法
2019/09/12 Javascript
vue.config.js中配置Vue的路径别名的方法
2020/02/11 Javascript
vue + el-form 实现的多层循环表单验证
2020/11/25 Vue.js
详解Python中time()方法的使用的教程
2015/05/22 Python
快速了解Python相对导入
2018/01/12 Python
python使用matplotlib库生成随机漫步图
2018/08/27 Python
详解安装mitmproxy以及遇到的坑和简单用法
2019/01/21 Python
pandas实现to_sql将DataFrame保存到数据库中
2019/07/03 Python
Python拼接字符串的7种方式详解
2020/03/19 Python
python实现密码强度校验
2020/03/18 Python
python输出国际象棋棋盘的实例分享
2020/11/26 Python
HTML5+CSS3模仿优酷视频截图功能示例
2017/01/05 HTML / CSS
HTML5通过navigator.mediaDevices.getUserMedia调用手机摄像头问题
2020/04/27 HTML / CSS
美国知名保健品网站:LuckyVitamin(支持中文)
2017/08/09 全球购物
英国男士时尚网站:Dandy Fellow
2018/02/09 全球购物
激励员工的口号
2014/06/16 职场文书
商务英语专业毕业生求职信
2014/07/06 职场文书
2014离婚协议书范文两篇
2014/09/15 职场文书
公司会议开幕词
2015/01/29 职场文书
公务员廉洁从政心得体会
2016/01/19 职场文书
第四次工业革命,打工人与机器人的竞争
2022/04/21 数码科技