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 相关文章推荐
连接到txt文本的超链接,不直接打开而是点击后下载的处理方法
Jul 01 PHP
PHP 写文本日志实现代码
May 18 PHP
php strstr查找字符串中是否包含某些字符的查找函数
Jun 03 PHP
PHP把网页保存为word文件的三种方法
Apr 01 PHP
ThinkPHP中url隐藏入口文件后接收alipay传值的方法
Dec 09 PHP
php截取指定2个字符之间字符串的方法
Apr 15 PHP
yii2控制器Controller Ajax操作示例
Jul 23 PHP
PHP入门教程之使用Mysqli操作数据库的方法(连接,查询,事务回滚等)
Sep 11 PHP
php输出图像的方法实例分析
Feb 16 PHP
PHP实现电商订单自动确认收货redis队列
May 17 PHP
PHP实现简单的模板引擎功能示例
Sep 02 PHP
ThinkPHP 框架实现的读取excel导入数据库操作示例
Apr 14 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 中move_uploaded_file 上传中文文件名失败
2019/04/17 PHP
(转载)JavaScript中匿名函数,函数直接量和闭包
2007/05/08 Javascript
服务器安全设置的几个注册表设置
2007/07/28 Javascript
犀利的js 函数集合
2009/06/11 Javascript
JavaScript编程中容易出BUG的几点小知识
2015/01/31 Javascript
jQuery实现点击后标记当前菜单位置(背景高亮菜单)效果
2015/08/22 Javascript
详解Wondows下Node.js使用MongoDB的环境配置
2016/03/01 Javascript
微信小程序模板和模块化用法实例分析
2017/11/28 Javascript
vue-cli 目录结构详细讲解总结
2019/01/15 Javascript
JavaScript刷新页面的几种方法总结
2019/03/28 Javascript
详解小程序中h5页面onShow实现及跨页面通信方案
2019/05/30 Javascript
vue canvas绘制矩形并解决由clearRec带来的闪屏问题
2019/09/02 Javascript
原生javascript制作的拼图游戏实现方法详解
2020/02/23 Javascript
JS实现炫酷轮播图
2020/11/15 Javascript
编写Python脚本使得web页面上的代码高亮显示
2015/04/24 Python
python编码最佳实践之总结
2016/02/14 Python
Python线程指南详细介绍
2017/01/05 Python
浅谈python之新式类
2018/08/12 Python
自定义django admin model表单提交的例子
2019/08/23 Python
Python中turtle库的使用实例
2019/09/09 Python
详解python中eval函数的作用
2019/10/22 Python
Python Django2 model 查询介绍(条件、范围、模糊查询)
2020/03/16 Python
python+appium+yaml移动端自动化测试框架实现详解
2020/11/24 Python
Wiggle中国:英国骑行、跑步、游泳 & 铁三运动装备专卖网店
2016/08/02 全球购物
加拿大时尚少女服装品牌:Garage
2016/10/10 全球购物
俄罗斯珠宝市场的领导者之一:Бронницкий ювелир
2019/10/02 全球购物
澳大利亚第一旅行车和房车配件店:Caravan RV Camping
2020/12/26 全球购物
如何配置、使用和清除Smarty缓存
2015/12/23 面试题
一个大学生十年的职业规划
2014/01/17 职场文书
语文教育专业求职信
2014/06/28 职场文书
国庆节促销广告语2014
2014/09/19 职场文书
放假通知格式
2015/04/14 职场文书
医疗纠纷调解协议书
2015/08/06 职场文书
小学班主任教育随笔
2015/08/15 职场文书
2016年“六一儿童节”校园广播稿
2015/12/17 职场文书
python如何读取.mtx文件
2021/04/22 Python