php实现微信公众号无限群发

本文给大家分享的是php实现的利用微信的客服接口进行各类消息的无限群发,思路非常巧妙,有需要的小伙伴可以参考下。

利用微信客服接口进行各类消息的无限群发

sendAllMsg.php

  1. <?php
  2. /*
  3. Author:yf
  4. 使用说明:微信公众号无限群发接口,使用实例:
  5. $test = new SendAllMsg("你的appId","你的appSecret");
  6. $test->sendMsgToAll(); //调用群发方法
  7. 注:1.使用条件:认证号或测试号
  8. 2.群发消息内容可为图文、文本、音乐等,$data具体内容参照微信开发文档/客服接口
  9. 3.若用户量过万,需修改getUserInfo(),具体参照信开发文档/获取关注者列表
  10. 新手上路,大神们多多指点,谢谢
  11. */
  12. interface iSendAllMsg{
  13. function getData($url); //curl 发送get请求
  14. function postData($url,$data); //curl 发送post请求
  15. function getAccessToken(); //在构造方法中已调用该方法来获取access_token,注意它在wx服务器的保存时间7200s
  16. function sendMsgToAll(); //群发消息方法,发送的消息$data 可自行修改
  17. }
  18. class SendAllMsg implements iSendAllMsg{
  19. private $appId;
  20. private $appSecret;
  21. private $access_token;
  22. //
  23. public function __construct($appId, $appSecret) {
  24. $this->appId = $appId;
  25. $this->appSecret = $appSecret;
  26. $this->access_token = $this->getAccessToken();
  27. }
  28. //
  29. function getData($url){
  30. $ch = curl_init();
  31. curl_setopt($ch, CURLOPT_URL, $url);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  33. curl_setopt($ch, CURLOPT_HEADER, 0);
  34. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  35. curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  37. $data = curl_exec($ch);
  38. curl_close($ch);
  39. return $data;
  40. }
  41. //
  42. function postData($url,$data){
  43. $ch = curl_init();
  44. curl_setopt($ch, CURLOPT_URL, $url);
  45. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  46. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  47. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  48. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  49. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  50. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  51. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  52. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  53. $tmpInfo = curl_exec($ch);
  54. if (curl_errno($ch)) {
  55. return curl_error($ch);
  56. }
  57. curl_close($ch);
  58. return $tmpInfo;
  59. }
  60. //
  61. function getAccessToken(){
  62. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&app&secret=".$this->appSecret;
  63. $res = $this->getData($url);
  64. $jres = json_decode($res,true);
  65. $access_token = $jres['access_token'];
  66. return $access_token;
  67. }
  68. //
  69. private function getUserInfo(){
  70. $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token;
  71. $res = $this->getData($url);
  72. $jres = json_decode($res,true);
  73. //print_r($jres);
  74. $userInfoList = $jres['data']['openid'];
  75. return $userInfoList;
  76. }
  77. function sendMsgToAll(){
  78. $userInfoList = $this->getUserInfo();
  79. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
  80. foreach($userInfoList as $val){
  81. $data = '{
  82. "touser":"'.$val.'",
  83. "msgtype":"text",
  84. "text":
  85. {
  86. "content":"测试一下,抱歉打扰各位"
  87. }
  88. }';
  89. $this->postData($url,$data);
  90. }
  91. }
  92. }
  93. $test = new SendAllMsg("YOURappId","YOURappSecret");
  94. $test->sendMsgToall();
  95. ?>

以上就是本文的全部内容了,希望大家能够喜欢。