Wechatext.class.php 27 KB

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