首页 > 开发 > PHP > 正文

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

2024-05-04 23:39:33
字体:
来源:转载
供稿:网友

本文给大家分享的是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. */ 
  13. interface iSendAllMsg{ 
  14. function getData($url); //curl 发送get请求 
  15. function postData($url,$data); //curl 发送post请求 
  16. function getAccessToken(); //在构造方法中已调用该方法来获取access_token,注意它在wx服务器的保存时间7200s 
  17. function sendMsgToAll(); //群发消息方法,发送的消息$data 可自行修改 
  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. 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. function postData($url,$data){ 
  41. $ch = curl_init(); 
  42. curl_setopt($ch, CURLOPT_URL, $url); 
  43. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
  44. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
  45. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
  46. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); 
  47. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  48. curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
  49. curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
  50. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  51. $tmpInfo = curl_exec($ch); 
  52. if (curl_errno($ch)) { 
  53. return curl_error($ch); 
  54. curl_close($ch); 
  55. return $tmpInfo; 
  56. // 
  57. function getAccessToken(){ 
  58. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret; 
  59. $res = $this->getData($url); 
  60. $jres = json_decode($res,true); 
  61. $access_token = $jres['access_token']; 
  62. return $access_token; 
  63. // 
  64. private function getUserInfo(){ 
  65. $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token; 
  66. $res = $this->getData($url); 
  67. $jres = json_decode($res,true); 
  68. //print_r($jres); 
  69. $userInfoList = $jres['data']['openid']; 
  70. return $userInfoList; 
  71. function sendMsgToAll(){ 
  72. $userInfoList = $this->getUserInfo(); 
  73. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token; 
  74. foreach($userInfoList as $val){ 
  75. $data = '{ 
  76. "touser":"'.$val.'"
  77. "msgtype":"text"
  78. "text"
  79. "content":"测试一下,抱歉打扰各位" 
  80. }'; 
  81. $this->postData($url,$data); 
  82. $test = new SendAllMsg("YOURappId","YOURappSecret"); 
  83. $test->sendMsgToall(); 
  84.  
  85. ?> 

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表