PHP實作登陸並抓取微信列表中最新一組微信訊息的方法詳解

墨辰丷
發布: 2023-03-26 21:38:02
原創
1387 人瀏覽過

這篇文章主要介紹了PHP實現登陸並抓取微信列表中最新一組微信訊息的方法,涉及php針對微信接口的登陸、抓取、轉換等相關操作技巧,需要的朋友可以參考下

<?php
$_G[&#39;wx_g&#39;] = array(&#39;init&#39; => array(
        "wx_content" => array("weixin_user" => "微信号码", "weixin_pass" => "微信密码")
      )
);
wx_login();
$messge_list = get_message_list();
$file_id=$messge_list[&#39;item&#39;][0][&#39;multi_item&#39;][0][&#39;file_id&#39;];
//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[&#39;item&#39;][0][&#39;multi_item&#39;] as $key => $val){
      $val[&#39;title&#39;]=mb_convert_encoding($val[&#39;title&#39;], &#39;GBK&#39;,&#39;UTF-8&#39;);
      $val[&#39;weiyi_id&#39;]=mb_convert_encoding($val[&#39;file_id&#39;], &#39;GBK&#39;,&#39;UTF-8&#39;);
      $val[&#39;des&#39;]=mb_convert_encoding($val[&#39;digest&#39;], &#39;GBK&#39;,&#39;UTF-8&#39;);
      $val[&#39;picurl&#39;]=$val[&#39;cover&#39;];
      $val[&#39;detail&#39;]=$val[&#39;content_url&#39;];
      $query_cheng = "INSERT INTO test.yangang_jiaojing(weiyi_id,title,pic_url,detail_url,des)VALUES ({$val[&#39;weiyi_id&#39;]},&#39;{$val[&#39;title&#39;]}&#39;,&#39;{$val[&#39;picurl&#39;]}&#39;,&#39;{$val[&#39;detail&#39;]}&#39;,&#39;{$val[&#39;des&#39;]}&#39;)";
      $count1=DB::query($query_cheng);
  }
}
function get_message_list(){
  global $_G;
  $cookie=$_G[&#39;wx_g&#39;][&#39;cookie&#39;];
  $url = "https://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=2&t=media/appmsg_list&type=10&action=list&token=".$_G[&#39;wx_g&#39;][&#39;token&#39;]."&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[&#39;wx_g&#39;][&#39;token&#39;]."&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(&#39;wx.cgiData = &#39;,$output2);
  $output1=$output1[1];
  $output1=explode(&#39;,"file_cnt":&#39;,$output1);
  $output1=$output1[0];
  $output1.=&#39;}&#39;;
  $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[&#39;wx_g&#39;][&#39;init&#39;][&#39;wx_content&#39;][&#39;weixin_user&#39;];exit;
  $username = $_G[&#39;wx_g&#39;][&#39;init&#39;][&#39;wx_content&#39;][&#39;weixin_user&#39;];
  $pwd = md5($_G[&#39;wx_g&#39;][&#39;init&#39;][&#39;wx_content&#39;][&#39;weixin_pass&#39;]);
  $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(&#39;Path=/&#39;,&#39; ; Secure; HttpOnly&#39;,&#39;=;&#39;),array(&#39;&#39;,&#39;&#39;,&#39;=&#39;), $cookie);
  $cookie = &#39;pgv_pvid=6648492946;&#39;.$cookie;
  $data = json_decode($body,true);
  $result = explode(&#39;token=&#39;,$data[&#39;redirect_url&#39;]);
  $token = $result[1];
  if(!$token) cpmsg($installlang[&#39;import_error_password&#39;], "{$request_url}&step=import&pswerror=1", &#39;error&#39;);
  //写入到全局变量
  $_G[&#39;wx_g&#39;][&#39;cookie&#39;] = $cookie;
  $_G[&#39;wx_g&#39;][&#39;token&#39;] = $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的laravel框架快速整合

微信登入的方法

以上是PHP實作登陸並抓取微信列表中最新一組微信訊息的方法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!