搜尋
首頁後端開發php教程微信訊息發送

}}
WeChat 사용자 로그인을 시뮬레이션하여 사용자 그룹을 얻고 메시지를 보냅니다.
  1. error_reporting( E_ALL ^ ​​​​E_NOTICE );
  2. // 사용 지침:
  3. // 로그인하여 시작
  4. $ param = array( );
  5. $param['username'] = '사용자 이름';
  6. $param['pwd'] = 'password';
  7. echo '
    '; 
  8. $wx = new Weixin();
  9. $flag = $wx->login($param);
  10. echo "Login:n";
  11. var_dump($ 플래그);
  12. echo "n";
  13. echo "그룹 가져오기:n";
  14. $group = $wx->getGroup();
  15. var_dump($group);
  16. echo "n";
  17. echo "그룹 구성원:n";
  18. $group = $wx->getFriendByGroup('0');
  19. var_dump($group);
  20. echo "n";
  21. echo "최신 뉴스 n";
  22. $msg = $wx->newmesg();
  23. var_dump($msg);
  24. echo "n" ;
  25. echo "이미지 및 텍스트 가져오기:n";
  26. $mesg = $wx->getMsg();
  27. var_dump($mesg);
  28. echo "n ";
  29. echo "메시지 보내기:n";
  30. // 설명: $content가 텍스트인 경우 텍스트 메시지를 보냅니다.
  31. // 설명: $content가 그래픽 ID인 경우 그래픽 메시지
  32. / /$content = '테스트 텍스트'; // 텍스트
  33. //$content = '10000023'; // 그래픽 자료 ID
  34. //$mesg = $wx->battchMesgByGroup( '101', $ content);
  35. //var_dump($mesg);
  36. $arr = array(
  37. 'fakeId'=>'985865180',
  38. "nickName"=>"鄄jintao",
  39. "remarkName"=>'',
  40. 'content'=>'10000002'
  41. );
  42. $s=$wx->sendmesg($arr);
  43. var_dump($ s);
  44. echo "df";
  45. /**
  46. * 위챗 퍼블릭 플랫폼 운영
  47. *PHP-CURL 기준
  48. *
  49. * @author phpbin
  50. *
  51. */
  52. class Weixin
  53. {
  54. /**
  55. * PHP 컬 헤더 부분
  56. *
  57. * @var array
  58. * /
  59. private $ _header;
  60. /**
  61. * 통신 쿠키
  62. *
  63. * @var string
  64. */
  65. private $_cookie;
  66. /**
  67. * 토큰
  68. *
  69. * @var 문자열
  70. */
  71. private $_token;
  72. /**
  73. * 초기화, 헤더 설정
  74. */
  75. 공개 함수 __construct()
  76. {
  77. $this->_header = array();
  78. $this->_header[ ] = "호스트:mp .weixin.qq.com";
  79. $this->_header[] = "추천자:https://mp.weixin.qq.com/cgi-bin/getmessage";
  80. }
  81. /**
  82. * 사용자 로그인
  83. * 구조 $param = array('username'=>'', 'pwd'=>'');
  84. *
  85. * @param 배열 $param
  86. * @return 부울
  87. */
  88. 공개 함수 로그인($param)
  89. {
  90. $url = 'https://mp.weixin.qq.com/cgi -bin/login?lang =zh_CN';
  91. $post = 'username='.urlencode($param['username']).'&pwd='.md5($param['pwd']).'&imgcode =&f=json';
  92. $stream = $this->_html($url, $post);
  93. // 로그인 성공 여부 확인
  94. $html = preg_replace("/ ^.*{/is" , "{", $stream);
  95. $json = json_decode($html, true);
  96. //토큰 가져오기
  97. preg_match("/lang=zh_CN&token=(d )/is", $json ['ErrMsg'], $match);
  98. $this->_token = $match[1];
  99. // 쿠키 가져오기
  100. $this-> ;_cookie($stream);
  101. return (boolean)$this->_token;
  102. }
  103. /**
  104. * 그래픽 메시지 받기
  105. *
  106. * @return array
  107. */
  108. 공개 함수 getMsg()
  109. {
  110. $url = 'https://mp.weixin.qq.com/cgi-bin/operate_appmsg?token='.$this->_token.'&lang=zh_CN&sub=list&type=10&subtype=3&t=wxm- appmsgs-list-new&pagesize=10&pageidx =0&lang=zh_CN';
  111. $stream = $this->_html($url);
  112. // 그룹 내 친구 분석
  113. preg_match_all('/ "appId":"(d ) ".*?"제목":"(.*?)".*?/is', $stream, $matches);
  114. if ( !is_array($matches[1] )) false 반환;
  115. $returns = array();
  116. foreach ( $matches[1] as $key=>$val) {
  117. $temp = array();
  118. $returns[$matches[1 ][$key]] = $matches[2][$key];
  119. }
  120. return $returns;
  121. }
  122. /**
  123. * 플랫폼 그룹 가져오기
  124. *
  125. * @return 배열
  126. */
  127. 공개 함수 getGroup()
  128. {
  129. $url = 'https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=10&pageidx=0&type =0&groupid=0&token='.$ this->_token.'&lang=zh_CN';
  130. $stream = $this->_html($url);
  131. // 그룹
  132. preg_match ('/"groups":( .*?)\}).groups/is', $stream, $match);
  133. $jsonArr = json_decode($match[1], true);
  134. $returns = array();
  135. foreach ( $jsonArr as $key=>$val) {
  136. $returns[$val['id']] = $val['name'].'('.$ val['cnt'].' )'
  137. }
  138. $returns;
  139. }
  140. /**
  141. * 取得分組成員
  142. *
  143. * @param integer $gId
  144. * @return array;
  145. */
  146. public function getFriendByGroup($gId)
  147. {
  148. $url = 'https://mp.weixin.qq.com/cgi-bin /contactmanage?t=user/index&pagesize=10&pageidx=0&type=0&groupid='.$gId.'&token='.$this->_token.'&lang=zh_CN';
  149. $stream = $this->_html ($ url);
  150. // 分析分組中好友
  151. preg_match('/"contacts":(.*?)\}).contacts/is', $stream, $match);
  152. $jsonArr = json_decode($match[1], true);
  153. if ( !is_array($jsonArr)) return false;
  154. $returns = array();
  155. foreach (
  156. $returns = array();
  157. foreach (
  158. $returns = array();
  159. foreach ( $jsonArr as $key=>$val) {
  160. $temp = array();
  161. $temp['fakeId'] = $val['id'];
  162. $temp['nickName'] = $val['nick_name'];
  163. $temp['remarkName'] = $val['remark_name'];
  164. $returns[] = $temp;
  165. }
  166. return $returns;
  167. }
  168. /**
  169. * 批次按群組傳送
  170. *
  171. * @param integer $gId 分組ID
  172. * @param string $content
  173. * @return array
  174. */
  175. public function battchMesgByGroup($gId, $content)
  176. {
  177. $meb = $this->getFriendByroup ($meb) ;
  178. if ( false == $mebInfo) return false;
  179. // 迴圈傳送
  180. $returns = array();
  181. foreach ( $mebInfoas $key=>> $val)
  182. {
  183. $val['content'] = $content;
  184. $this->sendmesg($val) ? $returns['succ'] : $returns['err'] ;
  185. }
  186. return $returns;
  187. }
  188. /**
  189. * 傳送訊息
  190. *
  191. * 結構 $param = array(fakeId, content, msgId);
  192. * @param array $param
  193. * @return boolean
  194. */
  195. public function sendmesg($param)
  196. {
  197. $url = 'https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response';
  198. // 分割型別進行個體
  199. if ( (int)$param['content']>100000)
  200. {
  201. $post = 'error=false&tofakeid='.$param['fakeId']. '&type=10&fid='.$param['content'].'&appmsgid='.$param['content'].'&quickreplyid='.$param['msgId'].'&token='.$this-> ;_token.'&ajax=1';
  202. } else {
  203. $post = 'error=false&tofakeid='.$param['fakeId'].'&type=1&content='.$param['content'] .'&quickreplyid='.$param['msgId'].'&token='.$this->_token.'&ajax=1';
  204. }
  205. $this->_header[1 ] = "引用者:https://mp.weixin.qq.com/cgi-bin/singlemsgpage?msgid=&source=&count=20&t=wxm-singlechat&fromfakeid=".$param['fakeId']."&token=". $this->_token;
  206. $stream = $this->_html($url, $post);
  207. // 不是設定成功
  208. $html = preg_replace("/^ .*{ /is", "{", $stream);
  209. $json = json_decode($html, true);
  210. return (boolean)$json['msg'] == 'ok';
  211. }
  212. /**
  213. * 從Stream擷取cookie
  214. *
  215. * @param string $stream
  216. */
  217. 陰道函數_cookie($stream)
  218. {
  219. preg_match_all("/Set-Cookie: (.*?);/is ",?);/is ", $流,$matches);
  220. $this->_cookie = @implode(";", $matches[1]);
  221. }
  222. /**
  223. * 取得Stream
  224. *
  225. * @param string $url
  226. * @param string $post
  227. * @return mix
  228. */
  229. 中斷函數_html($ url , $post = FALSE)
  230. {
  231. ob_start();
  232. $ch =curl_init($url);
  233. curl_setopt($ch, CURLOPT_HEADER, true);
  234. curl_setopt($ ) , CURLOPT_HTTPHEADER, $this->_header);
  235. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  236. if ( $post){
  237. curl_setopt($ch, CURLOPT, , true ( $ch, CURLOPT_POSTFIELDS, $post);
  238. }
  239. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  240. curl_setopt($ch, CURLOPT_COOKel,thisse_COOKp. ( $ch, CURLOPT_PROXY, 'http://10.100.10.100:3128');
  241. curl_exec($ch);
  242. curl_close($ch);
  243. $_str = ob_get_contents();
  244. $_str = ob_get_contents();
  245. ob_end_clean();
  246. return $_str;
  247. }
  248. /**
  249. * 取得最新消息
  250. *
  251. * 回傳結構:id:msgId; fakeId; nickName; content;
  252. *
  253. * @return array
  254. */
  255. }
  256. /***/
  257. public function newmesg()
  258. {
  259. $url = 'https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&token='.$this - > _token;
  260. $stream = $this->_html($url);
  261. preg_match('/"msg_item":(.*?)\}).msg_item/ is', $流, $match);
  262. $jsonArr = json_decode($match[1], true);
  263. $returns = array();
  264. foreach ( $jsonArr as $val ){
  265. if ( isset($val['is_starred_msg'])) continue;
  266. $returns[] = $val;
}
return $returns; }}
return $returns;
複製程式碼


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP中的依賴注入:避免常見的陷阱PHP中的依賴注入:避免常見的陷阱May 16, 2025 am 12:17 AM

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

如何加快PHP網站:性能調整如何加快PHP網站:性能調整May 16, 2025 am 12:12 AM

到Improveyourphpwebsite的實力,UsEthestertate:1)emplastOpCodeCachingWithOpcachetCachetOspeedUpScriptInterpretation.2)優化的atabasequesquesquesquelies berselectingOnlynlynnellynnessaryfields.3)usecachingsystemssslikeremememememcachedisemcachedtoredtoredtoredsatabaseloadch.4)

通過PHP發送大規模電子郵件:有可能嗎?通過PHP發送大規模電子郵件:有可能嗎?May 16, 2025 am 12:10 AM

是的,ItispossibletosendMassemailswithp.1)uselibrarieslikeLikePhpMailerorSwiftMailerForeffitedEmailsending.2)enasledeLaysBetenemailstoavoidSpamflagssspamflags.3))

PHP中依賴注入的目的是什麼?PHP中依賴注入的目的是什麼?May 16, 2025 am 12:10 AM

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

如何使用PHP發送電子郵件?如何使用PHP發送電子郵件?May 16, 2025 am 12:03 AM

使用PHP發送電子郵件的最佳方法包括:1.使用PHP的mail()函數進行基本發送;2.使用PHPMailer庫發送更複雜的HTML郵件;3.使用SendGrid等事務性郵件服務提高可靠性和分析能力。通過這些方法,可以確保郵件不僅到達收件箱,還能吸引收件人。

如何計算PHP多維數組的元素總數?如何計算PHP多維數組的元素總數?May 15, 2025 pm 09:00 PM

計算PHP多維數組的元素總數可以使用遞歸或迭代方法。 1.遞歸方法通過遍歷數組並遞歸處理嵌套數組來計數。 2.迭代方法使用棧來模擬遞歸,避免深度問題。 3.array_walk_recursive函數也能實現,但需手動計數。

PHP中do-while循環有什麼特點?PHP中do-while循環有什麼特點?May 15, 2025 pm 08:57 PM

在PHP中,do-while循環的特點是保證循環體至少執行一次,然後再根據條件決定是否繼續循環。 1)它在條件檢查之前執行循環體,適合需要確保操作至少執行一次的場景,如用戶輸入驗證和菜單系統。 2)然而,do-while循環的語法可能導致新手困惑,且可能增加不必要的性能開銷。

PHP中如何哈希字符串?PHP中如何哈希字符串?May 15, 2025 pm 08:54 PM

在PHP中高效地哈希字符串可以使用以下方法:1.使用md5函數進行快速哈希,但不適合密碼存儲。 2.使用sha256函數提高安全性。 3.使用password_hash函數處理密碼,提供最高安全性和便捷性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境