微信公眾號無限群發

來源:互聯網
上載者:User
利用客服介面進行各類訊息的無限群發
  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. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.