php实现微信公众号无限群发
网络编程 2021-07-05 09:09www.168986.cn编程入门
本文给大家分享的是php实现的利用微信的客服接口进行各类消息的无限群发,思路非常巧妙,有需要的小伙伴可以参考下
利用微信客服接口进行各类消息的无限群发
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 getAessToken(); //在构造方法中已调用该方法来获取aess_token,注意它在wx服务器的保存时间7200s function sendMsgToAll(); //群发消息方法,发送的消息$data 可自行修改 } class SendAllMsg implements iSendAllMsg{ private $appId; private $appSecret; private $aess_token; // public function __construct($appId, $appSecret) { $this->appId = $appId; $this->appSecret = $appSecret; $this->aess_token = $this->getAessToken(); } // function getData($url){ $ch = curl_init(); curl_set($ch, CURLOPT_URL, $url); curl_set($ch, CURLOPT_RETURNTRANSFER, 1); curl_set($ch, CURLOPT_HEADER, 0); curl_set($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (patible; MSIE 5.01; Windows NT 5.0)'); curl_set($ch, CURLOPT_ENCODING, 'gzip'); curl_set($ch, CURLOPT_SSL_VERIFYPEER, false); $data = curl_exec($ch); curl_close($ch); return $data; } // function postData($url,$data){ $ch = curl_init(); curl_set($ch, CURLOPT_URL, $url); curl_set($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_set($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_set($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_set($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (patible; MSIE 5.01; Windows NT 5.0)'); curl_set($ch, CURLOPT_FOLLOWLOCATION, 1); curl_set($ch, CURLOPT_AUTOREFERER, 1); curl_set($ch, CURLOPT_POSTFIELDS, $data); curl_set($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } curl_close($ch); return $tmpInfo; } // function getAessToken(){ $url = "https://api.weixin.qq./cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret; $res = $this->getData($url); $jres = json_decode($res,true); $aess_token = $jres['aess_token']; return $aess_token; } // private function getUserInfo(){ $url = "https://api.weixin.qq./cgi-bin/user/get?aess_token=".$this->aess_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./cgi-bin/message/custom/send?aess_token=".$this->aess_token; foreach($userInfoList as $val){ $data = '{ "touser":"'.$val.'", "msgtype":"text", "text": { "content":"测试一下,抱歉打扰各位" } }'; $this->postData($url,$data); } } } $test = new SendAllMsg("YOURappId","YOURappSecret"); $test->sendMsgToall(); ?>
以上就是本文的全部内容了,希望大家能够喜欢。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指