Home > Backend Development > PHP Tutorial > Unlimited group messaging on WeChat public accounts

Unlimited group messaging on WeChat public accounts

WBOY
Release: 2016-07-25 08:46:39
Original
1281 people have browsed it
Utilize WeChat customer service interface for unlimited mass sending of various messages
  1. ? /*
  2. Author:yf
  3. Instructions for use: WeChat public account wireless group sending interface, usage example:
  4. $test = new SendAllMsg("your appId", "your appSecret");
  5. $test->sendMsgToAll(); //Call the mass sending method
  6. Note: 1. Conditions of use: certification number or test number
  7. 2. The content of the mass message can be graphics, text, music, etc. For the specific content of $data, please refer to WeChat Development Documents/Customer Service Interface
  8. 3. If the number of users exceeds 10,000, you need to modify getUserInfo(). For details, please refer to the development documents/get follower list
  9. Newbies are starting to get started, please give your advice, thank you
  10. */
  11. interface iSendAllMsg{
  12. function getData($url); //curl sends a get request
  13. function postData($url,$data); //curl sends a post request
  14. function getAccessToken(); //This method has been called in the construction method to obtain access_token, please note Its storage time on the wx server is 7200s
  15. function sendMsgToAll(); //Group messaging method, the message $data sent can be modified by yourself
  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":"Test it, sorry to bother you"
  86. }
  87. }';
  88. $this ->postData($url,$data);
  89. }
  90. }
  91. }
  92. $test = new SendAllMsg("YOURappId","YOURappSecret");
  93. $test->sendMsgToall();
  94. ?>
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template