PHP實(shí)現(xiàn)登陸并抓取微信列表中最新一組微信消息的方法
本文實(shí)例講述了PHP實(shí)現(xiàn)登陸并抓取微信列表中最新一組微信消息的方法。分享給大家供大家參考,具體如下:
<?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 ;
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP微信開發(fā)技巧匯總》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- PHP后臺實(shí)現(xiàn)微信小程序登錄
- PHP實(shí)現(xiàn)微信小程序人臉識別刷臉登錄功能
- php的laravel框架快速集成微信登錄的方法
- PHP版微信第三方實(shí)現(xiàn)一鍵登錄及獲取用戶信息的方法
- php版微信自動登錄并獲取昵稱的方法
- PHP Curl模擬登錄微信公眾平臺、新浪微博實(shí)例代碼
- php實(shí)現(xiàn)微信掃碼自動登陸與注冊功能
- PHP實(shí)現(xiàn)微信模擬登陸并給用戶發(fā)送消息的方法【文字,圖片,圖文】
- php實(shí)現(xiàn)微信模擬登陸、獲取用戶列表及群發(fā)消息功能示例
- PHP微信PC二維碼登陸的實(shí)現(xiàn)思路
- PHP實(shí)現(xiàn)網(wǎng)站應(yīng)用微信登錄功能詳解
相關(guān)文章
PHP7原生MySQL數(shù)據(jù)庫操作實(shí)現(xiàn)代碼
php7已經(jīng)沒有mysql這個擴(kuò)展了,好多貼還是按照以前的方法講,讓我這個新手來回找了好多遍都沒找到mysql,php7用后面兩個鏈接數(shù)據(jù)庫,千萬不要傻傻去找也不要去來回拷貝什么dll之類的2020-07-07
PHP實(shí)現(xiàn)上傳多圖即時顯示與即時刪除的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)上傳多圖即時顯示與即時刪除的方法,結(jié)合實(shí)例形式分析了php針對圖片文件的預(yù)覽、上傳及刪除相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
總結(jié)的一些PHP開發(fā)中的tips(必看篇)
下面小編就為大家?guī)硪黄偨Y(jié)的一些PHP開發(fā)中的tips(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
php中print(),print_r(),echo()的區(qū)別詳解
本文介紹了php函數(shù)print(),print_r(),echo()的區(qū)別,這里推薦給大家,需要的朋友可以參考下2014-12-12
Windows下wamp php單元測試工具PHPUnit安裝及生成日志文件配置方法
這篇文章主要介紹了Windows下wamp php單元測試工具PHPUnit安裝及生成日志文件配置方法,簡明扼要的分析了Windows環(huán)境下wamp中php單元測試工具PHPUnit的安裝步驟、操作注意事項(xiàng)以及生成日志文件配置方法,需要的朋友可以參考下2018-05-05

