国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php實現(xiàn)微信公眾號無限群發(fā)

2024-05-04 23:39:33
字體:
供稿:網(wǎng)友

本文給大家分享的是php實現(xiàn)的利用微信的客服接口進(jìn)行各類消息的無限群發(fā),思路非常巧妙,有需要的小伙伴可以參考下

利用微信客服接口進(jìn)行各類消息的無限群發(fā)

sendAllMsg.php

 

 
  1. ?<?php 
  2. /* 
  3. Author:yf 
  4. 使用說明:微信公眾號無限群發(fā)接口,使用實例:  
  5. $test = new SendAllMsg("你的appId","你的appSecret"); 
  6. $test->sendMsgToAll(); //調(diào)用群發(fā)方法 
  7. 注:1.使用條件:認(rèn)證號或測試號 
  8. 2.群發(fā)消息內(nèi)容可為圖文、文本、音樂等,$data具體內(nèi)容參照微信開發(fā)文檔/客服接口 
  9. 3.若用戶量過萬,需修改getUserInfo(),具體參照信開發(fā)文檔/獲取關(guān)注者列表 
  10.  
  11. 新手上路,大神們多多指點,謝謝 
  12. */ 
  13. interface iSendAllMsg{ 
  14. function getData($url); //curl 發(fā)送get請求 
  15. function postData($url,$data); //curl 發(fā)送post請求 
  16. function getAccessToken(); //在構(gòu)造方法中已調(diào)用該方法來獲取access_token,注意它在wx服務(wù)器的保存時間7200s 
  17. function sendMsgToAll(); //群發(fā)消息方法,發(fā)送的消息$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. ?> 

以上就是本文的全部內(nèi)容了,希望大家能夠喜歡。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 桦川县| 梁山县| 根河市| 浦江县| 兴文县| 万安县| 蓬莱市| 宜兰县| 义乌市| 米林县| 西华县| 武川县| 江津市| 桓台县| 子长县| 罗城| 永嘉县| 汕尾市| 宣汉县| 仪陇县| 横峰县| 丹寨县| 周宁县| 茶陵县| 丰宁| 和政县| 通辽市| 临漳县| 娄烦县| 二连浩特市| 如东县| 无极县| 东乡族自治县| 都兰县| 惠来县| 榕江县| 克拉玛依市| 中江县| 南雄市| 庄河市| 平泉县|