当前位置:Gxlcms > PHP教程 > 微信信息发送

微信信息发送

时间:2021-07-01 10:21:17 帮助过:38人阅读

模拟登陆微信用户获取用户分组、发送消息
  1. error_reporting( E_ALL ^ E_NOTICE );
  2. // 使用说明:
  3. // 开始要登录
  4. $param = array();
  5. $param['username'] = '用户名';
  6. $param['pwd'] = '密码';
  7. echo '
    ';
  8. $wx = new Weixin();
  9. $flag = $wx->login($param);
  10. echo "登录:\n";
  11. var_dump($flag);
  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"=>"逄锦涛",
  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 curl头部分
  56. *
  57. * @var array
  58. */
  59. private $_header;
  60. /**
  61. * 通讯cookie
  62. *
  63. * @var string
  64. */
  65. private $_cookie;
  66. /**
  67. * 令牌
  68. *
  69. * @var string
  70. */
  71. private $_token;
  72. /**
  73. * 初始化,设置header
  74. */
  75. public function __construct()
  76. {
  77. $this->_header = array();
  78. $this->_header[] = "Host:mp.weixin.qq.com";
  79. $this->_header[] = "Referer:https://mp.weixin.qq.com/cgi-bin/getmessage";
  80. }
  81. /**
  82. * 用户登录
  83. * 结构 $param = array('username'=>'', 'pwd'=>'');
  84. *
  85. * @param array $param
  86. * @return boolean
  87. */
  88. public function login($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. //获取 token
  97. preg_match("/lang=zh_CN&token=(\d+)/is", $json['ErrMsg'], $match);
  98. $this->_token = $match[1];
  99. // 获取cookie
  100. $this->_cookie($stream);
  101. return (boolean)$this->_token;
  102. }
  103. /**
  104. * 获取图文消息
  105. *
  106. * @return array
  107. */
  108. public function 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+)".*?"title"\:"(.*?)".*?/is', $stream, $matches);
  114. if ( !is_array($matches[1])) return 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 array
  126. */
  127. public function 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. return $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 ( $jsonArr as $key=>$val) {
  156. $temp = array();
  157. $temp['fakeId'] = $val['id'];
  158. $temp['nickName'] = $val['nick_name'];
  159. $temp['remarkName'] = $val['remark_name'];
  160. $returns[] = $temp;
  161. }
  162. return $returns;
  163. }
  164. /**
  165. * 批量按组发送
  166. *
  167. * @param integer $gId 分组ID
  168. * @param string $content
  169. * @return array
  170. */
  171. public function battchMesgByGroup($gId, $content)
  172. {
  173. $mebInfo = $this->getFriendByGroup($gId);
  174. if ( false == $mebInfo) return false;
  175. // 循环发送
  176. $returns = array();
  177. foreach ( $mebInfo as $key=>$val)
  178. {
  179. $val['content'] = $content;
  180. $this->sendmesg($val) ? $returns['succ'] ++ : $returns['err']++;
  181. }
  182. return $returns;
  183. }
  184. /**
  185. * 发送消息
  186. *
  187. * 结构 $param = array(fakeId, content, msgId);
  188. * @param array $param
  189. * @return boolean
  190. */
  191. public function sendmesg($param)
  192. {
  193. $url = 'https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response';
  194. // 分类型进行推送
  195. if ( (int)$param['content']>100000)
  196. {
  197. $post = 'error=false&tofakeid='.$param['fakeId'].'&type=10&fid='.$param['content'].'&appmsgid='.$param['content'].'&quickreplyid='.$param['msgId'].'&token='.$this->_token.'&ajax=1';
  198. } else {
  199. $post = 'error=false&tofakeid='.$param['fakeId'].'&type=1&content='.$param['content'].'&quickreplyid='.$param['msgId'].'&token='.$this->_token.'&ajax=1';
  200. }
  201. $this->_header[1] = "Referer:https://mp.weixin.qq.com/cgi-bin/singlemsgpage?msgid=&source=&count=20&t=wxm-singlechat&fromfakeid=".$param['fakeId']."&token=".$this->_token;
  202. $stream = $this->_html($url, $post);
  203. // 是不是设置成功
  204. $html = preg_replace("/^.*\{/is", "{", $stream);
  205. $json = json_decode($html, true);
  206. return (boolean)$json['msg'] == 'ok';
  207. }
  208. /**
  209. * 从Stream中提取cookie
  210. *
  211. * @param string $stream
  212. */
  213. private function _cookie($stream)
  214. {
  215. preg_match_all("/Set-Cookie: (.*?);/is", $stream, $matches);
  216. $this->_cookie = @implode(";", $matches[1]);
  217. }
  218. /**
  219. * 获取Stream
  220. *
  221. * @param string $url
  222. * @param string $post
  223. * @return mixed
  224. */
  225. private function _html($url, $post = FALSE)
  226. {
  227. ob_start();
  228. $ch = curl_init($url);
  229. curl_setopt($ch, CURLOPT_HEADER, true);
  230. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_header);
  231. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  232. if ( $post){
  233. curl_setopt($ch, CURLOPT_POST, true);
  234. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  235. }
  236. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  237. curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie);
  238. //curl_setopt($ch, CURLOPT_PROXY, 'http://10.100.10.100:3128');
  239. curl_exec($ch);
  240. curl_close($ch);
  241. $_str = ob_get_contents();
  242. $_str = str_replace("script", "", $_str);
  243. ob_end_clean();
  244. return $_str;
  245. }
  246. /**
  247. * 获取最新消息
  248. *
  249. * 返回结构:id:msgId; fakeId; nickName; content;
  250. *
  251. * @return array
  252. */
  253. public function newmesg()
  254. {
  255. $url = 'https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&token='.$this->_token;
  256. $stream = $this->_html($url);
  257. preg_match('/"msg_item"\:(.*?)\\}\).msg_item/is', $stream, $match);
  258. $jsonArr = json_decode($match[1], true);
  259. $returns = array();
  260. foreach ( $jsonArr as $val){
  261. if ( isset($val['is_starred_msg'])) continue;
  262. $returns[] = $val;
  263. }
  264. return $returns;
  265. }
  266. }

人气教程排行