WeChat Public Number Unlimited mass

Source: Internet
Author: User
Unlimited mass of all kinds of messages using the Customer service interface
  1. ? /*
  2. Author:yf
  3. Instruction for use: Public number wireless mass interface, use example:
  4. $test = new Sendallmsg ("Your AppID", "Your Appsecret");
  5. $test->sendmsgtoall (); Call Mass method
  6. Note: 1. Conditions of Use: Certification number or test number
  7. 2. Mass message content for graphics, text, music, etc., $data specific content Reference development document/Customer service interface
  8. 3. If the user is over million, you need to modify GetUserInfo (), specific reference letter development document/Get followers list
  9. Novice on the road, the great gods, many pointers, 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 constructor method to obtain the Access_token, note that it is saved at WX server 7200s
  15. function Sendmsgtoall (); Mass message method, the message sent $data can be modified by itself
  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 disturb you."
  86. }
  87. }';
  88. $this->postdata ($url, $data);
  89. }
  90. }
  91. }
  92. $test = new Sendallmsg ("Yourappid", "Yourappsecret");
  93. $test->sendmsgtoall ();
  94. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.