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 相关文章推荐
array_multisort实现PHP多维数组排序示例讲解
Jan 04 PHP
PHP读取ACCESS数据到MYSQL的代码
May 11 PHP
PHP中strtotime函数使用方法详解
Nov 27 PHP
PHP高级对象构建 多个构造函数的使用
Feb 05 PHP
DOM XPATH获取img src值的query
Sep 23 PHP
php中调用其他系统http接口的方法说明
Feb 28 PHP
PHP实现下载断点续传的方法
Nov 12 PHP
Laravel 5 框架入门(四)完结篇
Apr 09 PHP
PHP+jQuery+Ajax实现分页效果 jPaginate插件的应用
Oct 09 PHP
PHP汉字转换拼音的函数代码
Dec 30 PHP
php类的自动加载操作实例详解
Sep 28 PHP
使用SMB共享来绕过php远程文件包含的限制执行RFI的利用
May 31 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
ip签名探针
2006/10/09 PHP
用PHP生成excel文件到指定目录
2015/06/22 PHP
PHP实现生成模糊图片的方法示例
2017/12/21 PHP
使用正则替换变量
2007/05/05 Javascript
jQuery 开天辟地入门篇一
2009/12/09 Javascript
基于jquery tab切换(防止页面刷新)
2012/05/23 Javascript
高性能Javascript笔记 数据的存储与访问性能优化
2012/08/02 Javascript
js中hash和ico的关联分析
2015/02/05 Javascript
浅谈Jquery核心函数
2015/06/18 Javascript
jQuery实现的指纹扫描效果实例(附演示与demo源码下载)
2016/01/26 Javascript
javascript获取wx.config内部字段解决微信分享
2016/03/09 Javascript
JavaScript面试题大全(推荐)
2016/09/22 Javascript
使用jquery.qrcode.js生成二维码插件
2016/10/17 Javascript
jQuery+Ajax请求本地数据加载商品列表页并跳转详情页的实现方法
2017/07/12 jQuery
vue Render中slots的使用的实例代码
2017/07/19 Javascript
jQuery实现获取table中鼠标click点击位置行号与列号的方法
2017/10/09 jQuery
Bootstrap table使用方法汇总
2017/11/17 Javascript
Vue  webpack 项目自动打包压缩成zip文件的方法
2019/07/24 Javascript
bootstrap-paginator服务器端分页使用方法详解
2020/02/13 Javascript
ES6扩展运算符和rest运算符用法实例分析
2020/05/23 Javascript
python实现电子词典
2020/04/23 Python
python数据结构链表之单向链表(实例讲解)
2017/07/25 Python
python实现支付宝当面付(扫码支付)功能
2018/05/30 Python
详解Python安装tesserocr遇到的各种问题及解决办法
2019/03/07 Python
python实现多进程通信实例分析
2019/09/01 Python
Python获取excel内容及相关操作代码实例
2020/08/10 Python
css3 flex布局 justify-content:space-between 最后一行左对齐
2020/01/02 HTML / CSS
HTML5 canvas基本绘图之文字渲染
2016/06/27 HTML / CSS
党校自我鉴定范文
2013/10/02 职场文书
酒店服务与管理毕业生求职信
2013/11/02 职场文书
11月红领巾广播稿
2014/01/17 职场文书
追悼会子女答谢词
2014/01/28 职场文书
2014年学习雷锋活动总结
2014/03/01 职场文书
奠基仪式主持词
2014/03/20 职场文书
小学生暑假家长评语
2014/04/17 职场文书
住宅质量保证书
2014/04/29 职场文书