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


Posted in PHP onJuly 10, 2017

本文实例讲述了PHP实现登陆并抓取微信列表中最新一组微信消息的方法。分享给大家供大家参考,具体如下:

<?php
$_G['wx_g'] = array('init' => array(
        "wx_content" => array("weixin_user" => "微信号码", "weixin_pass" => "微信密码")
      )
);
wx_login();
$messge_list = get_message_list();
$file_id=$messge_list['item'][0]['multi_item'][0]['file_id'];
//print_r($messge_list);exit;
if(!DB::result_first("select count(weiyi_id) from test.yangang_jiaojing where weiyi_id={$file_id} ")){
  DB::query("delete from test.yangang_jiaojing");
  foreach ($messge_list['item'][0]['multi_item'] as $key => $val){
      $val['title']=mb_convert_encoding($val['title'], 'GBK','UTF-8');
      $val['weiyi_id']=mb_convert_encoding($val['file_id'], 'GBK','UTF-8');
      $val['des']=mb_convert_encoding($val['digest'], 'GBK','UTF-8');
      $val['picurl']=$val['cover'];
      $val['detail']=$val['content_url'];
      $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']}')";
      $count1=DB::query($query_cheng);
  }
}
function get_message_list(){
  global $_G;
  $cookie=$_G['wx_g']['cookie'];
  $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";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  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");
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0");
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_SSLVERSION, 3);
  $output2 = curl_exec($ch);
  curl_close($ch);
  //echo $output2;exit;
  $output1=explode('wx.cgiData = ',$output2);
  $output1=$output1[1];
  $output1=explode(',"file_cnt":',$output1);
  $output1=$output1[0];
  $output1.='}';
  $message_list=json_decode($output1,true);
  //$message_list=mb_convert_encoding($message_list, "GBK","UTF-8");
  //print_r($message_list);exit;
  return $message_list;
}
function wx_login(){
  global $_G;
  //echo $_G['wx_g']['init']['wx_content']['weixin_user'];exit;
  $username = $_G['wx_g']['init']['wx_content']['weixin_user'];
  $pwd = md5($_G['wx_g']['init']['wx_content']['weixin_pass']);
  $url = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
  $post_data = "username=".$username."&pwd=".$pwd."&imgcode=&f=json";
  $cookie = "pgv_pvid=2067516646";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  curl_setopt($ch, CURLOPT_REFERER, "https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN");
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0");
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_SSLVERSION, 3);
  $output = curl_exec($ch);
  curl_close($ch);
  //echo $output;exit;
  list($header, $body) = explode("\r\n\r\n", $output);
  preg_match_all("/set\-cookie:([^\r\n]*)/i", $header, $matches);
  if(!empty($matches[1][2])){
    $cookie = $matches[1][0].$matches[1][1].$matches[1][2].$matches[1][3];
  }else{
    $cookie = $matches[1][0].$matches[1][1];
  }
  $cookie = str_replace(array('Path=/',' ; Secure; HttpOnly','=;'),array('','','='), $cookie);
  $cookie = 'pgv_pvid=6648492946;'.$cookie;
  $data = json_decode($body,true);
  $result = explode('token=',$data['redirect_url']);
  $token = $result[1];
  if(!$token) cpmsg($installlang['import_error_password'], "{$request_url}&step=import&pswerror=1", 'error');
  //写入到全局变量
  $_G['wx_g']['cookie'] = $cookie;
  $_G['wx_g']['token'] = $token;
}
?>
CREATE TABLE IF NOT EXISTS `yangang_jiaojing` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `title` varchar(100) NOT NULL,
 `des` varchar(300) NOT NULL,
 `detail_url` varchar(300) NOT NULL,
 `pic_url` varchar(300) NOT NULL,
 `note` varchar(50) NOT NULL,
 `weiyi_id` int(11) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
安装APACHE
Jan 15 PHP
PHP开发的一些注意点总结
Oct 12 PHP
解析PHP中如何将数组变量写入文件
Jun 06 PHP
PHP5中GD库生成图形验证码(有汉字)
Jul 28 PHP
PHP输出缓存ob系列函数详解
Mar 11 PHP
php-redis中的sort排序函数总结
Jul 08 PHP
PHP实现的迷你漂流瓶
Jul 29 PHP
php英文单词统计器
Jun 23 PHP
PHP记录和读取JSON格式日志文件
Jul 07 PHP
php实现简单加入购物车功能
Mar 07 PHP
PHP排序算法之快速排序(Quick Sort)及其优化算法详解
Apr 21 PHP
Mac下关于PHP环境和扩展的安装详解
Oct 17 PHP
PHP基于socket实现的简单客户端和服务端通讯功能示例
Jul 10 #PHP
PHP正则匹配操作简单示例【preg_match_all应用】
Jul 10 #PHP
php使用flock阻塞写入文件和非阻塞写入文件的实例讲解
Jul 10 #PHP
form自动提交实例讲解
Jul 10 #PHP
利用php的ob缓存机制实现页面静态化方法
Jul 09 #PHP
解决安装WampServer时提示缺少msvcr110.dll文件的问题
Jul 09 #PHP
详解php中serialize()和unserialize()函数
Jul 08 #PHP
You might like
全面解析PHP面向对象的三大特征
2017/06/10 PHP
Laravel 简单实现Ajax滚动加载示例
2019/10/22 PHP
javascript 获取HTML DOM父、子、临近节点
2014/06/16 Javascript
JS实现黑客帝国文字下落效果
2015/09/01 Javascript
request请求获取参数的实现方法(post和get两种方式)
2016/09/27 Javascript
jquery checkbox的相关操作总结
2016/10/17 Javascript
jQuery简单绑定单个事件的方法示例
2017/06/10 jQuery
微信小程序联网请求的轮播图
2017/07/07 Javascript
简单实现js鼠标跟随效果
2020/08/02 Javascript
jQuery获取复选框选中的当前行的某个字段的值
2017/09/15 jQuery
Angular2实现组件交互的方法分析
2017/12/19 Javascript
JS module的导出和导入的实现代码
2019/02/25 Javascript
vue中的inject学习教程
2019/04/24 Javascript
JQuery中DOM节点的操作与访问方法实例分析
2019/12/23 jQuery
Typescript3.9 常用新特性一览(推荐)
2020/05/14 Javascript
Python程序员鲜为人知但你应该知道的17个问题
2014/06/04 Python
Python StringIO模块实现在内存缓冲区中读写数据
2015/04/08 Python
Python实现快速排序算法及去重的快速排序的简单示例
2016/06/26 Python
python爬虫_微信公众号推送信息爬取的实例
2017/10/23 Python
对Django项目中的ORM映射与模糊查询的使用详解
2019/07/18 Python
selenium+PhantomJS爬取豆瓣读书
2019/08/26 Python
Django配置MySQL数据库的完整步骤
2019/09/07 Python
Python使用贪婪算法解决问题
2019/10/22 Python
HTML5 拖放功能实现代码
2016/07/14 HTML / CSS
Jeep牧马人、切诺基和自由人零配件:4 Wheel Drive Hardware
2017/07/02 全球购物
乌克兰在线商店的价格比较:Price.ua
2019/07/26 全球购物
工程专业毕业生自荐信范文
2013/12/25 职场文书
四年级学生期末评语
2014/12/26 职场文书
考研英语复习计划
2015/01/19 职场文书
2015年全国爱眼日活动小结
2015/02/27 职场文书
语文教师求职信范文
2015/03/20 职场文书
学校实习推荐信
2015/03/27 职场文书
观后感格式
2015/06/19 职场文书
pygame面向对象的飞行小鸟实现(Flappy bird)
2021/04/01 Python
JS ES6异步解决方案
2021/04/29 Javascript
Go timer如何调度
2021/06/09 Golang