wechatext.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <?php
  2. /**
  3. * 微信公众平台PHP-SDK
  4. * Wechatext为非官方微信发送API
  5. * 注: 用户id为通过getMsg()方法获取的FakeId值
  6. * 主要实现如下功能:
  7. * send($id,$content) 向某用户id发送微信文字信息
  8. * getUserList($page,$pagesize,$group) 获取用户信息
  9. * getGroupList($page,$pagesize) 获取群组信息
  10. * sendNews($id,$msgid) 发送图文消息
  11. * getNewsList($page,$pagesize) 获取图文信息列表
  12. * uploadFile($filepath,$type) 上传附件,包括图片/音频/视频
  13. * addPreview($title,$author,$summary,$content,$photoid,$srcurl='') 创建新的图文信息
  14. * getFileList($type,$page,$pagesize) 获取素材库文件列表
  15. * sendImage($id,$fid) 发送图片消息
  16. * sendAudio($id,$fid) 发送音频消息
  17. * sendVideo($id,$fid) 发送视频消息
  18. * getInfo($id) 根据id获取用户资料
  19. * getNewMsgNum($lastid) 获取从$lastid算起新消息的数目
  20. * getTopMsg() 获取最新一条消息的数据, 此方法获取的消息id可以作为检测新消息的$lastid依据
  21. * getMsg($lastid,$offset=0,$perpage=50,$day=0,$today=0,$star=0) 获取最新的消息列表, 列表将返回消息id, 用户id, 消息类型, 文字消息等参数
  22. * 消息返回结构: {"id":"消息id","type":"类型号(1为文字,2为图片,3为语音)","fileId":"0","hasReply":"0","fakeId":"用户uid","nickName":"昵称","dateTime":"时间戳","content":"文字内容"}
  23. * getMsgImage($msgid,$mode='large') 若消息type类型为2, 调用此方法获取图片数据
  24. * getMsgVoice($msgid) 若消息type类型为3, 调用此方法获取语音数据
  25. * @author dodge <dodgepudding@gmail.com>
  26. * @link https://github.com/dodgepudding/wechat-php-sdk
  27. * @version 1.2
  28. *
  29. */
  30. include "snoopy.class.php";
  31. class Wechatext
  32. {
  33. private $cookie;
  34. private $_cookiename;
  35. private $_cookieexpired = 3600;
  36. private $_account;
  37. private $_password;
  38. private $_datapath = './data/cookie_';
  39. private $debug;
  40. private $_logcallback;
  41. private $_token;
  42. public function __construct($options)
  43. {
  44. $this->_account = isset($options['account'])?$options['account']:'';
  45. $this->_password = isset($options['password'])?$options['password']:'';
  46. $this->_datapath = isset($options['datapath'])?$options['datapath']:$this->_datapath;
  47. $this->debug = isset($options['debug'])?$options['debug']:false;
  48. $this->_logcallback = isset($options['logcallback'])?$options['logcallback']:false;
  49. $this->_cookiename = $this->_datapath.$this->_account;
  50. $this->cookie = $this->getCookie($this->_cookiename);
  51. }
  52. /**
  53. * 主动发消息
  54. * @param string $id 用户的uid(即FakeId)
  55. * @param string $content 发送的内容
  56. */
  57. public function send($id,$content)
  58. {
  59. $send_snoopy = new Snoopy;
  60. $post = array();
  61. $post['tofakeid'] = $id;
  62. $post['type'] = 1;
  63. $post['token'] = $this->_token;
  64. $post['content'] = $content;
  65. $post['ajax'] = 1;
  66. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid=$id&token={$this->_token}&lang=zh_CN";
  67. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  68. $submit = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
  69. $send_snoopy->submit($submit,$post);
  70. $this->log($send_snoopy->results);
  71. return $send_snoopy->results;
  72. }
  73. /**
  74. * 群发功能 纯文本
  75. * @param string $content
  76. * @return string
  77. */
  78. public function mass($content) {
  79. $send_snoopy = new Snoopy;
  80. $post = array();
  81. $post['type'] = 1;
  82. $post['token'] = $this->_token;
  83. $post['content'] = $content;
  84. $post['ajax'] = 1;
  85. $post['city']='';
  86. $post['country']='';
  87. $post['f']='json';
  88. $post['groupid']='-1';
  89. $post['imgcode']='';
  90. $post['lang']='zh_CN';
  91. $post['province']='';
  92. $post['random']= rand(0, 1);
  93. $post['sex']=0;
  94. $post['synctxnews']=0;
  95. $post['synctxweibo']=0;
  96. $post['t']='ajax-response';
  97. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token={$this->_token}&lang=zh_CN";
  98. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  99. $submit = "https://mp.weixin.qq.com/cgi-bin/masssend";
  100. $send_snoopy->submit($submit,$post);
  101. $this->log($send_snoopy->results);
  102. return $send_snoopy->results;
  103. }
  104. /**
  105. * 群发功能 图文素材
  106. * @param int $appmsgid 图文素材ID
  107. * @return string
  108. */
  109. function massNews($appmsgid){
  110. $send_snoopy = new Snoopy;
  111. $post = array();
  112. $post['type'] = 10;
  113. $post['token'] = $this->_token;
  114. $post['appmsgid'] = $appmsgid;
  115. $post['ajax'] = 1;
  116. $post['city']='';
  117. $post['country']='';
  118. $post['f']='json';
  119. $post['groupid']='-1';
  120. $post['imgcode']='';
  121. $post['lang']='zh_CN';
  122. $post['province']='';
  123. $post['random']= rand(0, 1);
  124. $post['sex']=0;
  125. $post['synctxnews']=0;
  126. $post['synctxweibo']=0;
  127. $post['t']='ajax-response';
  128. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token={$this->_token}&lang=zh_CN";
  129. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  130. $submit = "https://mp.weixin.qq.com/cgi-bin/masssend";
  131. $send_snoopy->submit($submit,$post);
  132. $this->log($send_snoopy->results);
  133. return $send_snoopy->results;
  134. }
  135. /**
  136. * 获取用户列表列表
  137. * @param $page 页码(从0开始)
  138. * @param $pagesize 每页大小
  139. * @param $groupid 分组id
  140. * @return array ({contacts:[{id:12345667,nick_name:"昵称",remark_name:"备注名",group_id:0},{}....]})
  141. */
  142. function getUserList($page=0,$pagesize=10,$groupid=0){
  143. $send_snoopy = new Snoopy;
  144. $t = time().strval(mt_rand(100,999));
  145. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=".$pagesize."&pageidx=".$page."&type=0&groupid=0&lang=zh_CN&token=".$this->_token;
  146. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  147. $submit = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=".$pagesize."&pageidx=".$page."&type=0&groupid=$groupid&lang=zh_CN&f=json&token=".$this->_token;
  148. $send_snoopy->fetch($submit);
  149. $result = $send_snoopy->results;
  150. $this->log('userlist:'.$result);
  151. $json = json_decode($result,true);
  152. if (isset($json['contact_list'])) {
  153. $json = json_decode($json['contact_list'],true);
  154. if (isset($json['contacts']))
  155. return $json['contacts'];
  156. }
  157. return false;
  158. }
  159. /**
  160. * 获取分组列表
  161. *
  162. */
  163. function getGroupList(){
  164. $send_snoopy = new Snoopy;
  165. $t = time().strval(mt_rand(100,999));
  166. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=10&pageidx=0&type=0&groupid=0&lang=zh_CN&token=".$this->_token;
  167. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  168. $submit = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=10&pageidx=0&type=0&groupid=0&lang=zh_CN&f=json&token=".$this->_token;
  169. $send_snoopy->fetch($submit);
  170. $result = $send_snoopy->results;
  171. $this->log('userlist:'.$result);
  172. $json = json_decode($result,true);
  173. if (isset($json['group_list'])){
  174. $json = json_decode($json['group_list'],true);
  175. if (isset($json['groups']))
  176. return $json['groups'];
  177. }
  178. return false;
  179. }
  180. /**
  181. * 获取图文信息列表
  182. * @param $page 页码(从0开始)
  183. * @param $pagesize 每页大小
  184. * @return array
  185. */
  186. public function getNewsList($page,$pagesize=10) {
  187. $send_snoopy = new Snoopy;
  188. $t = time().strval(mt_rand(100,999));
  189. $type=10;
  190. $begin = $page*$pagesize;
  191. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token=".$this->_token."&lang=zh_CN";
  192. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  193. $submit = "https://mp.weixin.qq.com/cgi-bin/appmsg?token=".$this->_token."&lang=zh_CN&type=$type&action=list&begin=$begin&count=$pagesize&f=json&random=0.".$t;
  194. $send_snoopy->fetch($submit);
  195. $result = $send_snoopy->results;
  196. $this->log('newslist:'.$result);
  197. $json = json_decode($result,true);
  198. if (isset($json['app_msg_info'])) {
  199. return $json['app_msg_info'];
  200. }
  201. return false;
  202. }
  203. /**
  204. * 获取与指定用户的对话内容
  205. * @param $fakeid
  206. * @return array
  207. */
  208. public function getDialogMsg($fakeid) {
  209. $send_snoopy = new Snoopy;
  210. $t = time().strval(mt_rand(100,999));
  211. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token=".$this->_token."&lang=zh_CN";
  212. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  213. $submit = "https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid=".$fakeid."&token=".$this->_token."&lang=zh_CN&f=json&random=".$t;
  214. $send_snoopy->fetch($submit);
  215. $result = $send_snoopy->results;
  216. $this->log('DialogMsg:'.$result);
  217. $json = json_decode($result,true);
  218. if (isset($json['page_info'])) {
  219. return $json['page_info'];
  220. }
  221. return false;
  222. }
  223. /**
  224. * 发送图文信息,必须从图文库里选取消息ID发送
  225. * @param string $id 用户的uid(即FakeId)
  226. * @param string $msgid 图文消息id
  227. */
  228. public function sendNews($id,$msgid)
  229. {
  230. $send_snoopy = new Snoopy;
  231. $post = array();
  232. $post['tofakeid'] = $id;
  233. $post['type'] = 10;
  234. $post['token'] = $this->_token;
  235. $post['fid'] = $msgid;
  236. $post['appmsgid'] = $msgid;
  237. $post['error'] = 'false';
  238. $post['ajax'] = 1;
  239. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/singlemsgpage?fromfakeid={$id}&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN";
  240. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  241. $submit = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
  242. $send_snoopy->submit($submit,$post);
  243. $this->log($send_snoopy->results);
  244. return $send_snoopy->results;
  245. }
  246. /**
  247. * 上传附件(图片/音频/视频)
  248. * @param string $filepath 本地文件地址
  249. * @param int $type 文件类型: 2:图片 3:音频 4:视频
  250. */
  251. public function uploadFile($filepath,$type=2) {
  252. $send_snoopy = new Snoopy;
  253. $send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-upload&lang=zh_CN&type=2&formId=1";
  254. $t = time().strval(mt_rand(100,999));
  255. $post = array('formId'=>'');
  256. $postfile = array('uploadfile'=>$filepath);
  257. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  258. $send_snoopy->set_submit_multipart();
  259. $submit = "http://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=$type&token=".$this->_token."&t=iframe-uploadfile&lang=zh_CN&formId= file_from_".$t;
  260. $send_snoopy->submit($submit,$post,$postfile);
  261. $tmp = $send_snoopy->results;
  262. $this->log('upload:'.$tmp);
  263. preg_match("/formId,.*?\'(\d+)\'/",$tmp,$matches);
  264. if (isset($matches[1])) {
  265. return $matches[1];
  266. }
  267. return false;
  268. }
  269. /**
  270. * 创建图文消息
  271. * @param array $title 标题
  272. * @param array $summary 摘要
  273. * @param array $content 内容
  274. * @param array $photoid 素材库里的图片id(可通过uploadFile上传后获取)
  275. * @param array $srcurl 原文链接
  276. * @return json
  277. */
  278. public function addPreview($title,$author,$summary,$content,$photoid,$srcurl='') {
  279. $send_snoopy = new Snoopy;
  280. $send_snoopy->referer = 'https://mp.weixin.qq.com/cgi-bin/operate_appmsg?lang=zh_CN&sub=edit&t=wxm-appmsgs-edit-new&type=10&subtype=3&token='.$this->_token;
  281. $submit = "https://mp.weixin.qq.com/cgi-bin/operate_appmsg?lang=zh_CN&t=ajax-response&sub=create&token=".$this->_token;
  282. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  283. $send_snoopy->set_submit_normal();
  284. $post = array(
  285. 'token'=>$this->_token,
  286. 'type'=>10,
  287. 'lang'=>'zh_CN',
  288. 'sub'=>'create',
  289. 'ajax'=>1,
  290. 'AppMsgId'=>'',
  291. 'error'=>'false',
  292. );
  293. if (count($title)==count($author)&&count($title)==count($summary)&&count($title)==count($content)&&count($title)==count($photoid))
  294. {
  295. $i = 0;
  296. foreach($title as $v) {
  297. $post['title'.$i] = $title[$i];
  298. $post['author'.$i] = $author[$i];
  299. $post['digest'.$i] = $summary[$i];
  300. $post['content'.$i] = $content[$i];
  301. $post['fileid'.$i] = $photoid[$i];
  302. if ($srcurl[$i]) $post['sourceurl'.$i] = $srcurl[$i];
  303. $i++;
  304. }
  305. }
  306. $post['count'] = $i;
  307. $post['token'] = $this->_token;
  308. $send_snoopy->submit($submit,$post);
  309. $tmp = $send_snoopy->results;
  310. $this->log('step2:'.$tmp);
  311. $json = json_decode($tmp,true);
  312. return $json;
  313. }
  314. /**
  315. * 发送媒体文件
  316. * @param $id 用户的uid(即FakeId)
  317. * @param $fid 文件id
  318. * @param $type 文件类型
  319. */
  320. public function sendFile($id,$fid,$type) {
  321. $send_snoopy = new Snoopy;
  322. $post = array();
  323. $post['tofakeid'] = $id;
  324. $post['type'] = $type;
  325. $post['token'] = $this->_token;
  326. $post['fid'] = $fid;
  327. $post['fileid'] = $fid;
  328. $post['error'] = 'false';
  329. $post['ajax'] = 1;
  330. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/singlemsgpage?fromfakeid={$id}&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN";
  331. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  332. $submit = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response";
  333. $send_snoopy->submit($submit,$post);
  334. $result = $send_snoopy->results;
  335. $this->log('sendfile:'.$result);
  336. $json = json_decode($result,true);
  337. if ($json && $json['ret']==0)
  338. return true;
  339. else
  340. return false;
  341. }
  342. /**
  343. * 获取素材库文件列表
  344. * @param $type 文件类型: 2:图片 3:音频 4:视频
  345. * @param $page 页码(从0开始)
  346. * @param $pagesize 每页大小
  347. * @return array
  348. */
  349. public function getFileList($type,$page,$pagesize=10) {
  350. $send_snoopy = new Snoopy;
  351. $t = time().strval(mt_rand(100,999));
  352. $begin = $page*$pagesize;
  353. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token=".$this->_token."&lang=zh_CN";
  354. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  355. $submit = "https://mp.weixin.qq.com/cgi-bin/filepage?token=".$this->_token."&lang=zh_CN&type=$type&random=0.".$t."&begin=$begin&count=$pagesize&f=json";
  356. $send_snoopy->fetch($submit);
  357. $result = $send_snoopy->results;
  358. $this->log('filelist:'.$result);
  359. $json = json_decode($result,true);
  360. if (isset($json['page_info']))
  361. return $json['page_info'];
  362. else
  363. return false;
  364. }
  365. /**
  366. * 发送图文信息,必须从库里选取文件ID发送
  367. * @param string $id 用户的uid(即FakeId)
  368. * @param string $fid 文件id
  369. */
  370. public function sendImage($id,$fid)
  371. {
  372. return $this->sendFile($id,$fid,2);
  373. }
  374. /**
  375. * 发送语音信息,必须从库里选取文件ID发送
  376. * @param string $id 用户的uid(即FakeId)
  377. * @param string $fid 语音文件id
  378. */
  379. public function sendAudio($id,$fid)
  380. {
  381. return $this->sendFile($id,$fid,3);
  382. }
  383. /**
  384. * 发送视频信息,必须从库里选取文件ID发送
  385. * @param string $id 用户的uid(即FakeId)
  386. * @param string $fid 视频文件id
  387. */
  388. public function sendVideo($id,$fid)
  389. {
  390. return $this->sendFile($id,$fid,4);
  391. }
  392. /**
  393. * 发送预览图文消息
  394. * @param string $account 账户名称(user_name)
  395. * @param string $title 标题
  396. * @param string $summary 摘要
  397. * @param string $content 内容
  398. * @param string $photoid 素材库里的图片id(可通过uploadFile上传后获取)
  399. * @param string $srcurl 原文链接
  400. * @return json
  401. */
  402. public function sendPreview($account,$title,$summary,$content,$photoid,$srcurl='') {
  403. $send_snoopy = new Snoopy;
  404. $submit = "https://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=preview&t=ajax-appmsg-preview";
  405. $send_snoopy->set_submit_normal();
  406. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  407. $send_snoopy->referer = 'https://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=edit&t=wxm-appmsgs-edit-new&type=10&subtype=3&lang=zh_CN';
  408. $post = array(
  409. 'AppMsgId'=>'',
  410. 'ajax'=>1,
  411. 'content0'=>$content,
  412. 'count'=>1,
  413. 'digest0'=>$summary,
  414. 'error'=>'false',
  415. 'fileid0'=>$photoid,
  416. 'preusername'=>$account,
  417. 'sourceurl0'=>$srcurl,
  418. 'title0'=>$title,
  419. );
  420. $post['token'] = $this->_token;
  421. $send_snoopy->submit($submit,$post);
  422. $tmp = $send_snoopy->results;
  423. $this->log('sendpreview:'.$tmp);
  424. $json = json_decode($tmp,true);
  425. return $json;
  426. }
  427. /**
  428. * 获取用户的信息
  429. * @param string $id 用户的uid(即FakeId)
  430. * @return array {fake_id:100001,nick_name:'昵称',user_name:'用户名',signature:'签名档',country:'中国',province:'广东',city:'广州',gender:'1',group_id:'0'},groups:{[id:0,name:'未分组',cnt:20]}
  431. */
  432. public function getInfo($id)
  433. {
  434. $send_snoopy = new Snoopy;
  435. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  436. $t = time().strval(mt_rand(100,999));
  437. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
  438. $submit = "https://mp.weixin.qq.com/cgi-bin/getcontactinfo";
  439. $post = array('ajax'=>1,'lang'=>'zh_CN','random'=>'0.'.$t,'token'=>$this->_token,'t'=>'ajax-getcontactinfo','fakeid'=>$id);
  440. $send_snoopy->submit($submit,$post);
  441. $this->log($send_snoopy->results);
  442. $result = json_decode($send_snoopy->results,true);
  443. if(isset($result['contact_info'])){
  444. return $result['contact_info'];
  445. }
  446. return false;
  447. }
  448. /**
  449. * 获得头像数据
  450. *
  451. * @param FakeId $fakeid
  452. * @return JPG二进制数据
  453. */
  454. public function getHeadImg($fakeid){
  455. $send_snoopy = new Snoopy;
  456. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  457. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
  458. $url = "https://mp.weixin.qq.com/misc/getheadimg?fakeid=$fakeid&token=".$this->_token."&lang=zh_CN";
  459. $send_snoopy->fetch($url);
  460. $result = $send_snoopy->results;
  461. $this->log('Head image:'.$fakeid.'; length:'.strlen($result));
  462. if(!$result){
  463. return false;
  464. }
  465. return $result;
  466. }
  467. /**
  468. * 获取消息更新数目
  469. * @param int $lastid 最近获取的消息ID,为0时获取总消息数目
  470. * @return int 数目
  471. */
  472. public function getNewMsgNum($lastid=0){
  473. $send_snoopy = new Snoopy;
  474. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  475. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
  476. $submit = "https://mp.weixin.qq.com/cgi-bin/getnewmsgnum?t=ajax-getmsgnum&lastmsgid=".$lastid;
  477. $post = array('ajax'=>1,'token'=>$this->_token);
  478. $send_snoopy->submit($submit,$post);
  479. $this->log($send_snoopy->results);
  480. $result = json_decode($send_snoopy->results,1);
  481. if(!$result){
  482. return false;
  483. }
  484. return intval($result['newTotalMsgCount']);
  485. }
  486. /**
  487. * 获取最新一条消息
  488. * @return array {"id":"最新一条id","type":"类型号(1为文字,2为图片,3为语音)","fileId":"0","hasReply":"0","fakeId":"用户uid","nickName":"昵称","dateTime":"时间戳","content":"文字内容","playLength":"0","length":"0","source":"","starred":"0","status":"4"}
  489. */
  490. public function getTopMsg(){
  491. $send_snoopy = new Snoopy;
  492. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  493. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&lang=zh_CN&token=".$this->_token;
  494. $submit = "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&f=json&count=20&day=7&lang=zh_CN&token=".$this->_token;
  495. $send_snoopy->fetch($submit);
  496. $this->log($send_snoopy->results);
  497. $result = $send_snoopy->results;
  498. $json = json_decode($result,true);
  499. if (isset($json['msg_items'])) {
  500. $json = json_decode($json['msg_items'],true);
  501. if(isset($json['msg_item']))
  502. return array_shift($json['msg_item']);
  503. }
  504. return false;
  505. }
  506. /**
  507. * 获取新消息
  508. * @param $lastid 传入最后的消息id编号,为0则从最新一条起倒序获取
  509. * @param $offset lastid起算第一条的偏移量
  510. * @param $perpage 每页获取多少条
  511. * @param $day 最近几天消息(0:今天,1:昨天,2:前天,3:更早,7:五天内)
  512. * @param $today 是否只显示今天的消息, 与$day参数不能同时大于0
  513. * @param $star 是否星标组信息
  514. * @return array[] 同getTopMsg()返回的字段结构相同
  515. */
  516. public function getMsg($lastid=0,$offset=0,$perpage=20,$day=7,$today=0,$star=0){
  517. $send_snoopy = new Snoopy;
  518. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  519. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&lang=zh_CN&count=50&token=".$this->_token;
  520. $lastid = $lastid===0 ? '':$lastid;
  521. $addstar = $star?'&action=star':'';
  522. $submit = "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&f=json&lang=zh_CN{$addstar}&count=$perpage&timeline=$today&day=$day&frommsgid=$lastid&offset=$offset&token=".$this->_token;
  523. $send_snoopy->fetch($submit);
  524. $this->log($send_snoopy->results);
  525. $result = $send_snoopy->results;
  526. $json = json_decode($result,true);
  527. if (isset($json['msg_items'])) {
  528. $json = json_decode($json['msg_items'],true);
  529. if(isset($json['msg_item']))
  530. return $json['msg_item'];
  531. }
  532. return false;
  533. }
  534. /**
  535. * 获取图片消息
  536. * @param int $msgid 消息id
  537. * @param string $mode 图片尺寸(large/small)
  538. * @return jpg二进制文件
  539. */
  540. public function getMsgImage($msgid,$mode='large'){
  541. $send_snoopy = new Snoopy;
  542. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  543. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
  544. $url = "https://mp.weixin.qq.com/cgi-bin/getimgdata?token=".$this->_token."&msgid=$msgid&mode=$mode&source=&fileId=0";
  545. $send_snoopy->fetch($url);
  546. $result = $send_snoopy->results;
  547. $this->log('msg image:'.$msgid.';length:'.strlen($result));
  548. if(!$result){
  549. return false;
  550. }
  551. return $result;
  552. }
  553. /**
  554. * 获取语音消息
  555. * @param int $msgid 消息id
  556. * @return mp3二进制文件
  557. */
  558. public function getMsgVoice($msgid){
  559. $send_snoopy = new Snoopy;
  560. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  561. $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&token=".$this->_token;
  562. $url = "https://mp.weixin.qq.com/cgi-bin/getvoicedata?token=".$this->_token."&msgid=$msgid&fileId=0";
  563. $send_snoopy->fetch($url);
  564. $result = $send_snoopy->results;
  565. $this->log('msg voice:'.$msgid.';length:'.strlen($result));
  566. if(!$result){
  567. return false;
  568. }
  569. return $result;
  570. }
  571. /**
  572. * 模拟登录获取cookie
  573. * @return [type] [description]
  574. */
  575. /**
  576. * 模拟登录获取cookie
  577. * @return [type] [description]
  578. */
  579. public function login(){
  580. $snoopy = new Snoopy;
  581. $submit = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
  582. $post["username"] = $this->_account;
  583. $post["pwd"] = md5($this->_password);
  584. $post["f"] = "json";
  585. $post["imgcode"] = "";
  586. $snoopy->referer = "https://mp.weixin.qq.com/";
  587. $snoopy->submit($submit,$post);
  588. $cookie = '';
  589. $this->log($snoopy->results);
  590. $result = json_decode($snoopy->results,true);
  591. if (!isset($result['base_resp']) || $result['base_resp']['ret'] != 0) {
  592. return false;
  593. }
  594. foreach ($snoopy->headers as $key => $value) {
  595. $value = trim($value);
  596. if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $value,$match))
  597. $cookie .=$match[1].'='.$match[2].'; ';
  598. }
  599. preg_match("/token=(\d+)/i",$result['redirect_url'],$matches);
  600. if($matches){
  601. $this->_token = $matches[1];
  602. $this->log('token:'.$this->_token);
  603. }
  604. $this->saveCookie($this->_cookiename,$cookie);
  605. return $cookie;
  606. }
  607. /**
  608. * 把cookie写入缓存
  609. * @param string $filename 缓存文件名
  610. * @param string $content 文件内容
  611. * @return bool
  612. */
  613. public function saveCookie($filename,$content){
  614. return file_put_contents($filename,$content);
  615. }
  616. /**
  617. * 读取cookie缓存内容
  618. * @param string $filename 缓存文件名
  619. * @return string cookie
  620. */
  621. public function getCookie($filename){
  622. if (file_exists($filename)) {
  623. $mtime = filemtime($filename);
  624. if ($mtime<time()-$this->_cookieexpired)
  625. $data = '';
  626. else
  627. $data = file_get_contents($filename);
  628. } else
  629. $data = '';
  630. if($data){
  631. $send_snoopy = new Snoopy;
  632. $send_snoopy->rawheaders['Cookie']= $data;
  633. $send_snoopy->maxredirs = 0;
  634. $url = "https://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
  635. $send_snoopy->fetch($url);
  636. $header = implode(',',$send_snoopy->headers);
  637. $this->log('header:'.print_r($send_snoopy->headers,true));
  638. preg_match("/token=(\d+)/i",$header,$matches);
  639. if(empty($matches)){
  640. return $this->login();
  641. }else{
  642. $this->_token = $matches[1];
  643. $this->log('token:'.$this->_token);
  644. return $data;
  645. }
  646. }else{
  647. return $this->login();
  648. }
  649. }
  650. /**
  651. * 验证cookie的有效性
  652. * @return bool
  653. */
  654. public function checkValid()
  655. {
  656. if (!$this->cookie || !$this->_token) return false;
  657. $send_snoopy = new Snoopy;
  658. $post = array('ajax'=>1,'token'=>$this->_token);
  659. $submit = "https://mp.weixin.qq.com/cgi-bin/getregions?id=1017&t=ajax-getregions&lang=zh_CN";
  660. $send_snoopy->rawheaders['Cookie']= $this->cookie;
  661. $send_snoopy->submit($submit,$post);
  662. $result = $send_snoopy->results;
  663. if(json_decode($result,1)){
  664. return true;
  665. }else{
  666. return false;
  667. }
  668. }
  669. private function log($log){
  670. if ($this->debug && function_exists($this->_logcallback)) {
  671. if (is_array($log)) $log = print_r($log,true);
  672. return call_user_func($this->_logcallback,$log);
  673. }
  674. }
  675. }