欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-12 08:12:56
本文实例讲述了php实现登陆并抓取微信列表中最新一组微信消息的方法。分享给大家供大家参考,具体如下:

本文实例讲述了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编码与转码操作技巧汇总》、《php网络编程技巧总结》、《php基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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