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+javascript模拟Matrix画面
Oct 09 PHP
图片存储与浏览一例(Linux+Apache+PHP+MySQL)
Oct 09 PHP
SSI指令
Nov 25 PHP
重新封装zend_soap实现http连接安全认证的php代码
Jan 12 PHP
PHP中使用memcache存储session的三种配置方法
Apr 05 PHP
PHP中iconv函数知识汇总
Jul 02 PHP
简单谈谈PHP中的Reload操作
Dec 12 PHP
详解EventDispatcher事件分发组件
Dec 25 PHP
PHP实现十进制、二进制、八进制和十六进制转换相关函数用法分析
Apr 25 PHP
PHP使用Redis长连接的方法详解
Feb 12 PHP
php7性能提升的原因详解
Oct 13 PHP
PHP二维数组分页2种实现方法解析
Jul 09 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/12/05 PHP
php实现按指定大小等比缩放生成上传图片缩略图的方法
2014/12/15 PHP
php自动给网址加上链接的方法
2015/06/02 PHP
使用php实现网站验证码功能【推荐】
2017/02/09 PHP
Yii2实现UploadedFile上传文件示例
2017/02/15 PHP
javascript 遍历验证所有文本框的值
2009/08/27 Javascript
Nodejs全栈框架StrongLoop推荐
2014/11/09 NodeJs
jquery中cookie用法实例详解(获取,存储,删除等)
2016/01/04 Javascript
bootstrap IE8 兼容性处理
2017/03/22 Javascript
Angular2使用Guard和Resolve进行验证和权限控制
2017/04/24 Javascript
神级程序员JavaScript300行代码搞定汉字转拼音
2017/05/20 Javascript
讲解vue-router之什么是动态路由
2018/05/28 Javascript
node.js遍历目录的方法示例
2018/08/01 Javascript
js的继承方法小结(prototype、call、apply)(推荐)
2019/04/17 Javascript
解决VUE 在IE下出现ReferenceError: Promise未定义的问题
2020/11/07 Javascript
[01:13]这,就是刀塔
2014/07/16 DOTA
python3使用tkinter实现ui界面简单实例
2014/01/10 Python
Django imgareaselect手动剪切头像实现方法
2015/05/26 Python
Flask 让jsonify返回的json串支持中文显示的方法
2018/03/26 Python
python用pandas数据加载、存储与文件格式的实例
2018/12/07 Python
对Python 两大环境管理神器 pyenv 和 virtualenv详解
2018/12/31 Python
Python configparser模块配置文件过程解析
2020/03/03 Python
西班牙Polo衫品牌:Polo Club
2020/08/09 全球购物
main 函数执行以前,还会执行什么代码
2013/04/17 面试题
高一英语教学反思
2014/01/22 职场文书
法人代表授权委托书
2014/04/08 职场文书
工地安全质量标语
2014/06/07 职场文书
教师民族团结演讲稿
2014/08/27 职场文书
仓管员岗位职责范本
2015/04/01 职场文书
新郎接新娘保证书
2015/05/08 职场文书
企业法人任命书
2015/09/21 职场文书
辞职信怎么写?
2019/05/21 职场文书
2019年XX公司的晨会制度及流程!
2019/07/23 职场文书
《最终幻想14》6.01版本4月5日推出 追加新任务新道具
2022/04/03 其他游戏
Mysql数据库事务的脏读幻读及不可重复读详解
2022/05/30 MySQL
JS轻量级函数式编程实现XDM三
2022/06/16 Javascript