当前位置:Gxlcms > PHP教程 > PHP实现登陆并抓取微信列表中最新一组微信消息的方法详解

PHP实现登陆并抓取微信列表中最新一组微信消息的方法详解

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

这篇文章主要介绍了PHP实现登陆并抓取微信列表中最新一组微信消息的方法,涉及php针对微信接口的登陆、抓取、转换等相关操作技巧,需要的朋友可以参考下

  1. <?php
  2. $_G['wx_g'] = array('init' => array(
  3. "wx_content" => array("weixin_user" => "微信号码", "weixin_pass" => "微信密码")
  4. )
  5. );
  6. wx_login();
  7. $messge_list = get_message_list();
  8. $file_id=$messge_list['item'][0]['multi_item'][0]['file_id'];
  9. //print_r($messge_list);exit;
  10. if(!DB::result_first("select count(weiyi_id) from test.yangang_jiaojing where weiyi_id={$file_id} ")){
  11. DB::query("delete from test.yangang_jiaojing");
  12. foreach ($messge_list['item'][0]['multi_item'] as $key => $val){
  13. $val['title']=mb_convert_encoding($val['title'], 'GBK','UTF-8');
  14. $val['weiyi_id']=mb_convert_encoding($val['file_id'], 'GBK','UTF-8');
  15. $val['des']=mb_convert_encoding($val['digest'], 'GBK','UTF-8');
  16. $val['picurl']=$val['cover'];
  17. $val['detail']=$val['content_url'];
  18. $query_cheng = "INSERT INTO test.yangang_jiaojing(weiyi_id,title,pic_url,detail_url,des)VALUES ({$val['weiyi_id']},'{$val['title']}','{$val['picurl']}','{$val['detail']}','{$val['des']}')";
  19. $count1=DB::query($query_cheng);
  20. }
  21. }
  22. function get_message_list(){
  23. global $_G;
  24. $cookie=$_G['wx_g']['cookie'];
  25. $url = "https://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=2&t=media/appmsg_list&type=10&action=list&token=".$_G['wx_g']['token']."&lang=zh_CN";
  26. $ch = curl_init();
  27. curl_setopt($ch, CURLOPT_URL, $url);
  28. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  29. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  30. curl_setopt($ch, CURLOPT_REFERER, "https://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=2&t=media/appmsg_list&type=10&action=list&token=".$_G['wx_g']['token']."&lang=zh_CN");
  31. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0");
  32. curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
  33. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  34. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  35. curl_setopt($ch, CURLOPT_SSLVERSION, 3);
  36. $output2 = curl_exec($ch);
  37. curl_close($ch);
  38. //echo $output2;exit;
  39. $output1=explode('wx.cgiData = ',$output2);
  40. $output1=$output1[1];
  41. $output1=explode(',"file_cnt":',$output1);
  42. $output1=$output1[0];
  43. $output1.='}';
  44. $message_list=json_decode($output1,true);
  45. //$message_list=mb_convert_encoding($message_list, "GBK","UTF-8");
  46. //print_r($message_list);exit;
  47. return $message_list;
  48. }
  49. function wx_login(){
  50. global $_G;
  51. //echo $_G['wx_g']['init']['wx_content']['weixin_user'];exit;
  52. $username = $_G['wx_g']['init']['wx_content']['weixin_user'];
  53. $pwd = md5($_G['wx_g']['init']['wx_content']['weixin_pass']);
  54. $url = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
  55. $post_data = "username=".$username."&pwd=".$pwd."&imgcode=&f=json";
  56. $cookie = "pgv_pvid=2067516646";
  57. $ch = curl_init();
  58. curl_setopt($ch, CURLOPT_URL, $url);
  59. curl_setopt($ch, CURLOPT_HEADER, 1);
  60. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  61. curl_setopt($ch, CURLOPT_POST, 1);
  62. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  63. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  64. curl_setopt($ch, CURLOPT_REFERER, "https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN");
  65. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0");
  66. curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
  67. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  68. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  69. curl_setopt($ch, CURLOPT_SSLVERSION, 3);
  70. $output = curl_exec($ch);
  71. curl_close($ch);
  72. //echo $output;exit;
  73. list($header, $body) = explode("\r\n\r\n", $output);
  74. preg_match_all("/set\-cookie:([^\r\n]*)/i", $header, $matches);
  75. if(!empty($matches[1][2])){
  76. $cookie = $matches[1][0].$matches[1][1].$matches[1][2].$matches[1][3];
  77. }else{
  78. $cookie = $matches[1][0].$matches[1][1];
  79. }
  80. $cookie = str_replace(array('Path=/',' ; Secure; HttpOnly','=;'),array('','','='), $cookie);
  81. $cookie = 'pgv_pvid=6648492946;'.$cookie;
  82. $data = json_decode($body,true);
  83. $result = explode('token=',$data['redirect_url']);
  84. $token = $result[1];
  85. if(!$token) cpmsg($installlang['import_error_password'], "{$request_url}&step=import&pswerror=1", 'error');
  86. //写入到全局变量
  87. $_G['wx_g']['cookie'] = $cookie;
  88. $_G['wx_g']['token'] = $token;
  89. }
  90. ?>

  1. CREATE TABLE IF NOT EXISTS `yangang_jiaojing` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `title` varchar(100) NOT NULL,
  4. `des` varchar(300) NOT NULL,
  5. `detail_url` varchar(300) NOT NULL,
  6. `pic_url` varchar(300) NOT NULL,
  7. `note` varchar(50) NOT NULL,
  8. `weiyi_id` int(11) NOT NULL,
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;

相关推荐:

PHP网站应用微信登录的方法

自定义微信登录扫码样式解决办法

php的laravel框架快速集成微信登录的方法

以上就是PHP实现登陆并抓取微信列表中最新一组微信消息的方法详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行