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项目的方法
Oct 09 PHP
PHP脚本的10个技巧(6)
Oct 09 PHP
杏林同学录(九)
Oct 09 PHP
完美解决dedecms中的[html][/html]和[code][/code]问题
Mar 20 PHP
PHP编实现程动态图像的创建代码
Sep 28 PHP
基于PHP选项与信息函数的使用详解
May 10 PHP
php 获取本地IP代码
Jun 23 PHP
页面乱码问题的根源及其分析
Aug 09 PHP
php环境套包 dedeampz 伪静态设置示例
Mar 26 PHP
PHP错误Cannot use object of type stdClass as array in错误的解决办法
Jun 12 PHP
php结合ACCESS的跨库查询功能
Jun 12 PHP
PHP date()格式MySQL中插入datetime方法
Jan 29 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使用iconv中文截断问题的解决方法
2015/02/11 PHP
ThinkPHP框架实现数据增删改
2017/05/07 PHP
Prototype使用指南之selector.js
2007/01/10 Javascript
常用的javascript function代码
2008/05/23 Javascript
JQuery学习笔录 简单的JQuery
2012/04/09 Javascript
高效率JavaScript编写技巧整理
2013/08/23 Javascript
Egret引擎开发指南之创建项目
2014/09/03 Javascript
jQuery插件pagewalkthrough实现引导页效果
2015/07/05 Javascript
Jquery轮播效果实现过程解析
2016/03/30 Javascript
asp.net+jquery.form实现图片异步上传的方法(附jquery.form.js下载)
2016/05/05 Javascript
Bootstrap3学习笔记(二)之排版
2016/05/20 Javascript
js常用的继承--组合式继承
2017/03/06 Javascript
ReactNative实现图片上传功能的示例代码
2017/07/11 Javascript
Vue2.0 slot分发内容与props验证的方法
2017/12/12 Javascript
微信小程序iBeacon测距及稳定程序的实现解析
2019/07/31 Javascript
vue 计算属性和侦听器的使用小结
2021/01/25 Vue.js
JS获取一个字符串中指定字符串第n次出现的位置
2021/02/10 Javascript
Python中使用pprint函数进行格式化输出的教程
2015/04/07 Python
在Python的Django框架中显示对象子集的方法
2015/07/21 Python
详解python时间模块中的datetime模块
2016/01/13 Python
Python下实现的RSA加密/解密及签名/验证功能示例
2017/07/17 Python
Python模拟随机游走图形效果示例
2018/02/06 Python
pandas 数据归一化以及行删除例程的方法
2018/11/10 Python
python文件写入write()的操作
2019/05/14 Python
python 数据库查询返回list或tuple实例
2020/05/15 Python
python3 re返回形式总结
2020/11/20 Python
python实现简单的学生管理系统
2021/02/22 Python
仿CSDN Blog返回页面顶部功能实现原理及代码
2013/06/30 HTML / CSS
canvas学习总结三之绘制路径-线段
2019/01/31 HTML / CSS
松下电器美国官方商店:Panasonic美国
2016/10/14 全球购物
欧洲最大的品牌水上运动服装和设备在线零售商:Wuituit Outlet
2018/05/05 全球购物
土木工程毕业生推荐信
2013/10/28 职场文书
股权转让协议书
2014/12/07 职场文书
学期个人自我总结
2015/02/13 职场文书
2015年乡镇统计工作总结
2015/04/22 职场文书
小学班主任工作随笔
2015/08/15 职场文书