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 相关文章推荐
PHP+AJAX实现无刷新注册(带用户名实时检测)
Jan 02 PHP
PHP开启gzip页面压缩实例代码
Mar 11 PHP
php根据isbn书号查询amazon网站上的图书信息的示例
Feb 13 PHP
php随机显示图片的简单示例
Feb 15 PHP
php数组中删除元素之重新索引的方法
Sep 16 PHP
PHP判断数据库中的记录是否存在的方法
Nov 14 PHP
PHP封装CURL扩展类实例
Jul 28 PHP
PHP MVC框架路由学习笔记
Mar 02 PHP
PHP二分查找算法示例【递归与非递归方法】
Sep 29 PHP
ThinkPHP中类的构造函数_construct()与_initialize()的区别详解
Mar 13 PHP
PHP实现APP微信支付的实例讲解
Feb 10 PHP
tp5(thinkPHP5)框架数据库Db增删改查常见操作总结
Jan 10 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开发框架总结收藏
2008/04/24 PHP
php 删除数组元素
2009/01/16 PHP
我整理的PHP 7.0主要新特性
2016/01/07 PHP
阿里云PHP SMS短信服务验证码发送方法
2017/07/11 PHP
详解Laravel5.6 Passport实现Api接口认证
2018/07/27 PHP
Laravel框架使用monolog_mysql实现将系统日志信息保存到mysql数据库的方法
2018/08/16 PHP
找到了一篇jQuery与Prototype并存的冲突的解决方法
2007/08/29 Javascript
JS 文件本身编码转换 图文教程
2009/10/12 Javascript
jQuery ul标签下拉菜单演示代码
2010/12/11 Javascript
javascript 文件的同步加载与异步加载实现原理
2012/12/13 Javascript
基于jquery的手风琴图片展示效果实现方法
2014/12/16 Javascript
Node.js的包详细介绍
2015/01/14 Javascript
基于JS实现省市联动效果代码分享
2016/06/06 Javascript
JS 实现计算器详解及实例代码(一)
2017/01/08 Javascript
AngularJS之页面跳转Route实例代码
2017/03/10 Javascript
微信小程序获取手机号授权用户登录功能
2017/11/09 Javascript
jQuery实现遍历XML节点和属性的方法示例
2018/04/29 jQuery
简单了解JavaScript sort方法
2019/11/25 Javascript
Python中dictionary items()系列函数的用法实例
2014/08/21 Python
Pycharm远程调试openstack的方法
2017/11/21 Python
查看django执行的sql语句及消耗时间的两种方法
2018/05/29 Python
Python开发网站目录扫描器的实现
2019/02/21 Python
python3 反射的四种基本方法解析
2019/08/26 Python
python循环嵌套的多种使用方法解析
2019/11/29 Python
Python2.7:使用Pyhook模块监听鼠标键盘事件-获取坐标实例
2020/03/14 Python
HTML5新增的Css选择器、伪类介绍
2013/08/07 HTML / CSS
Peter Alexander新西兰站:澳大利亚领先的睡衣设计师品牌
2016/12/10 全球购物
Brookstone美国官网:独特新奇产品
2017/03/04 全球购物
英国第一家领先的在线处方眼镜零售商:Glasses Direct
2018/02/23 全球购物
优秀高中生事迹材料
2014/02/11 职场文书
年终总结会主持词
2014/03/25 职场文书
地震捐款倡议书
2014/08/29 职场文书
春节慰问信范文
2015/02/15 职场文书
2015应届毕业生求职信范文
2015/03/20 职场文书
铁头也玩根德 YachtBoy YB-230......
2022/04/05 无线电
golang操作redis的客户端包有多个比如redigo、go-redis
2022/04/14 Golang