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 相关文章推荐
PHPwind整合最土系统用户同步登录实现方法
Dec 08 PHP
Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)
Jun 14 PHP
win10环境PHP 7 安装配置【教程】
May 09 PHP
windows7配置Nginx+php+mysql的详细教程
Sep 04 PHP
PHP页面输出搜索后跳转下一页的处理方法
Sep 30 PHP
使用Yii2实现主从数据库设置
Nov 20 PHP
php文件管理基本功能简单操作
Jan 16 PHP
php获取文件名称和扩展名的方法
Feb 07 PHP
thinkphp ajaxfileupload实现异步上传图片的示例
Aug 28 PHP
thinkphp5实现无限级分类
Feb 18 PHP
laravel使用Faker数据填充的实现方法
Apr 12 PHP
PHP反射学习入门示例
Jun 14 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/03/27 PHP
CodeIgniter配置之SESSION用法实例分析
2016/01/19 PHP
thinkPHP5.0框架整体架构总览【应用,模块,MVC,驱动,行为,命名空间等】
2017/03/25 PHP
php多进程并发编程防止出现僵尸进程的方法分析
2020/02/28 PHP
jquery 表单取值常用代码
2009/12/22 Javascript
javascript json2 使用方法
2010/03/16 Javascript
js新闻滚动 js如何实现新闻滚动效果
2013/01/07 Javascript
根据json字符串生成Html的一种方式
2013/01/09 Javascript
判断滚动条到底部的JS代码
2013/11/04 Javascript
按下回车键指向下一个位置的一个函数代码
2014/03/10 Javascript
jquery等待效果示例
2014/05/01 Javascript
jQuery基于当前元素进行下一步的遍历
2014/05/20 Javascript
JS常用表单验证方法总结
2014/05/22 Javascript
使用jQuery判断IE浏览器版本的代码
2014/06/14 Javascript
JavaScript实现查找字符串中第一个不重复的字符
2014/12/29 Javascript
使用jquery获取url以及jquery获取url参数的实现方法
2016/05/25 Javascript
AngularJs  Understanding Angular Templates
2016/09/02 Javascript
微信小程序  http请求封装详解及实例代码
2017/02/15 Javascript
详解Typescript 内置的模块导入兼容方式
2020/05/31 Javascript
google广告之另类js调用实现代码
2020/08/22 Javascript
UEditor 自定义图片视频尺寸校验功能的实现代码
2020/10/20 Javascript
vue 获取url里参数的两种方法小结
2020/11/12 Javascript
微信小程序实现可拖动悬浮图标(包括按钮角标的实现)
2020/12/29 Javascript
简单的Apache+FastCGI+Django配置指南
2015/07/22 Python
Python中如何使用if语句处理列表实例代码
2019/02/24 Python
Python中SQLite如何使用
2020/05/27 Python
详解python UDP 编程
2020/08/24 Python
python 8种必备的gui库
2020/08/27 Python
瑰珀翠美国官网:Crabtree & Evelyn美国
2016/11/29 全球购物
Tea Collection官网:一家位于旧金山的童装公司
2020/08/07 全球购物
银行党的群众路线教育实践活动对照检查材料
2014/09/25 职场文书
暖春观后感
2015/06/08 职场文书
幼儿园卫生保健制度
2015/08/05 职场文书
一文搞懂python异常处理、模块与包
2021/06/26 Python
vscode中使用npm安装babel的方法
2021/08/02 Javascript
开机音效回归! Windows 11重新引入开机铃声
2021/11/21 数码科技