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 07 PHP
linux环境apache多端口配置虚拟主机的方法深入介绍
Jun 09 PHP
利用phpexcel把excel导入数据库和数据库导出excel实现
Jan 09 PHP
两级联动select刷新后其值保持不变的实现方法
Jan 27 PHP
PHP5各个版本的新功能和新特性总结
Mar 16 PHP
五款PHP代码重构工具推荐
Oct 14 PHP
如何解决PHP无法实现多线程的问题
Sep 25 PHP
CodeIgniter 完美解决URL含有中文字符串
May 13 PHP
PHP基于新浪IP库获取IP详细地址的方法
May 04 PHP
php框架CodeIgniter主从数据库配置方法分析
May 25 PHP
Laravel 6 将新增为指定队列任务设置中间件的功能
Aug 06 PHP
php使用redis的几种常见操作方式和用法示例
Feb 20 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字符转义相关函数小结(php下的转义字符串)
2007/04/12 PHP
PHP 生成微信红包代码简单
2016/03/25 PHP
PHP如何获取当前主机、域名、网址、路径、端口等参数
2017/06/09 PHP
PHP使用DOM对XML解析处理操作示例
2019/07/04 PHP
jquery tools 系列 scrollable学习
2009/09/06 Javascript
Ajax 数据请求的简单分析
2011/04/05 Javascript
jquery.blockUI.js上传滚动等待效果实现思路及代码
2013/03/18 Javascript
jQuery隔行变色与普通JS写法的对比
2013/04/21 Javascript
Jquery多选下拉列表插件jquery multiselect功能介绍及使用
2013/05/24 Javascript
js控制表单不能输入空格的小例子
2013/11/20 Javascript
JS实现跟随鼠标的链接文字提示框效果
2015/08/06 Javascript
jQuery on()方法示例及jquery on()方法的优点
2015/08/27 Javascript
动态设置form表单的action属性的值的简单方法
2016/05/25 Javascript
jQuery的层级查找方式分析
2016/06/16 Javascript
JavaScript实现输入框与清空按钮联动效果
2016/09/09 Javascript
js对字符串进行编码的方法总结(推荐)
2016/11/10 Javascript
String字符串截取的四种方式总结
2016/11/28 Javascript
详解使用grunt完成requirejs的合并压缩和js文件的版本控制
2017/03/02 Javascript
JS实现前端缓存的方法
2017/09/21 Javascript
Node实战之不同环境下配置文件使用教程
2018/01/02 Javascript
简单理解Vue中的nextTick方法
2018/01/30 Javascript
vue 1.0 结合animate.css定义动画效果
2018/07/11 Javascript
[59:30]完美世界DOTA2联赛PWL S3 access vs LBZS 第二场 12.20
2020/12/23 DOTA
用python实现的可以拷贝或剪切一个文件列表中的所有文件
2009/04/30 Python
Flask框架学习笔记之表单基础介绍与表单提交方式
2019/08/12 Python
python yield关键词案例测试
2019/10/15 Python
keras 实现轻量级网络ShuffleNet教程
2020/06/19 Python
python爬虫中的url下载器用法详解
2020/11/30 Python
解决pytorch 保存模型遇到的问题
2021/03/03 Python
HTML5实现移动端复制功能
2018/04/19 HTML / CSS
英国时尚高尔夫服装购物网站:Trendy Golf
2020/01/10 全球购物
数控专业大学生的自我鉴定
2013/11/13 职场文书
劳动竞赛活动总结
2014/05/05 职场文书
2014年社区居委会主任重阳节讲话稿
2014/09/25 职场文书
2014流动人口计划生育工作总结
2014/12/20 职场文书
小学三八妇女节活动总结
2015/02/06 职场文书