欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

微信公众号无限群发

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