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 相关文章推荐
PHP4引用文件语句的对比
Oct 09 PHP
php学习笔记 面向对象中[接口]与[多态性]的应用
Jun 16 PHP
用php随机生成福彩双色球号码的2种方法
Feb 04 PHP
CodeIgniter框架URL路由总结
Sep 03 PHP
使用php的HTTP请求的库Requests实现美女图片墙
Feb 22 PHP
php与Mysql的一些简单的操作
Feb 26 PHP
老司机传授Ubuntu下Apache+PHP+MySQL环境搭建攻略
Mar 20 PHP
php使用curl实现ftp文件下载功能
May 16 PHP
微信公众号实现会员卡领取功能
Jun 08 PHP
自定义Laravel (monolog)日志位置,并增加请求ID的实现
Oct 17 PHP
PHP For循环字母A-Z当超过26个字母时输出AA,AB,AC
Feb 16 PHP
ThinkPHP 5 AJAX跨域请求头设置实现过程解析
Oct 28 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_SELF的安全问题
2009/09/05 PHP
ajax在joomla中的原生态应用代码
2012/07/19 PHP
解析php addslashes()与addclashes()函数的区别和比较
2013/06/24 PHP
thinkphp文件引用与分支结构用法实例
2014/11/26 PHP
php绘图之加载外部图片的方法
2015/01/24 PHP
分享ThinkPHP3.2中关联查询解决思路
2015/09/20 PHP
微信公众平台DEMO(PHP)
2016/05/04 PHP
PHP实现QQ、微信和支付宝三合一收款码实例代码
2018/02/19 PHP
PHP自定义函数实现assign()数组分配到模板及extract()变量分配到模板功能示例
2018/05/23 PHP
php中curl和soap方式请求服务超时问题的解决
2018/06/11 PHP
ASP.NET jQuery 实例4(复制TextBox的文本到本地剪贴板上)
2012/01/13 Javascript
JavaScript学习笔记之Cookie对象
2015/01/22 Javascript
纯JavaScript代码实现文本比较工具
2016/02/17 Javascript
JavaScript如何实现组合列表框中元素移动效果
2016/03/01 Javascript
javascript 闭包详解及简单实例应用
2016/12/31 Javascript
详解Vuejs2.0之异步跨域请求
2017/04/20 Javascript
vue的状态管理模式vuex
2017/11/30 Javascript
用Electron写个带界面的nodejs爬虫的实现方法
2019/01/29 NodeJs
Python 爬虫图片简单实现
2017/06/01 Python
使用Python批量修改文件名的代码实例
2019/01/24 Python
PyQt4 treewidget 选择改变颜色,并设置可编辑的方法
2019/06/17 Python
python多进程(加入进程池)操作常见案例
2019/10/21 Python
Win10里python3创建虚拟环境的步骤
2020/01/31 Python
python对文件的操作方法汇总
2020/02/28 Python
python求解汉诺塔游戏
2020/07/09 Python
伦敦最著名的老字号百货公司:Selfridges(塞尔福里奇百货)
2016/07/25 全球购物
德国体育用品网上商店:SC24.com
2016/08/01 全球购物
JAVA程序员面试题
2012/10/03 面试题
产品工艺师的岗位职责
2013/11/15 职场文书
给领导的检讨书
2014/02/16 职场文书
教师作风建设剖析材料
2014/10/11 职场文书
初婚未育证明样本
2014/10/24 职场文书
吃空饷专项整治方案
2014/10/27 职场文书
三八妇女节慰问信
2015/02/14 职场文书
捐款仪式主持词
2015/07/04 职场文书
详解Vue的sync修饰符
2021/05/15 Vue.js