php模擬用戶自動(dòng)在qq空間發(fā)表文章的方法
本文實(shí)例講述了php模擬用戶自動(dòng)在qq空間發(fā)表文章的方法。分享給大家供大家參考。具體分析如下:
我們這里是一個(gè)簡(jiǎn)單的利用php來(lái)模擬登錄后再到QQ空間發(fā)送文章的一個(gè)簡(jiǎn)單的程序,有需要的朋友可以參考,或改進(jìn)可以給我意見,代碼如下:
//模擬get post請(qǐng)求函數(shù)
/*
函數(shù)說(shuō)明:
功能:請(qǐng)求方式可以get,post,可以發(fā)送的cookie,保存的cookiefile文件
參數(shù):$url-----請(qǐng)求url $referer---來(lái)源url $postdata----------用于post請(qǐng)求的數(shù)據(jù),''為get請(qǐng)求
$cookie---------發(fā)送的cookie $cookiefile-----保存的cookiefile文件
返回值:返回獲取的源碼
*/
function request($url,$referer='',$postdata='',$cookie='',$cookiefile=''){
//header設(shè)置
$header='';
$header.="Content-Type: application/x-www-form-urlencodedrn";//內(nèi)容請(qǐng)求類型
$header.="User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)rn";//瀏覽器字段
$header.="Referer:".$referer."rn";//設(shè)置來(lái)源地址
$header .= "Cookie:".$cookie ; //設(shè)置cookie,默認(rèn)空
//請(qǐng)求方法get post,通過(guò)$postdata空---get,非空----post
if($postdata=='')$method='GET';
else $method='POST';
//定義用于創(chuàng)建流的數(shù)組
$opts=array();
$opts['http']=array('method'=>$method,'header'=>$header,'content'=>$postdata);
//生成流
$context=stream_context_create($opts);
//發(fā)送請(qǐng)求,獲取源碼
$yuanma=file_get_contents($url,false,$context);
//是否需要保存cookie到文件,$cookiefile不空時(shí)
if($cookiefile!=''){
echo '需要保存cookie<br>';
//判斷保存文件存在,不存在創(chuàng)建
if(!file_exists($cookiefile)){
file_put_contents($cookiefile,'');
}
//獲取cookie,保存起來(lái)
$response=implode("rn",$http_response_header);
//用正則匹配cookie
$zengze="/Set-Cookie:(.*?)rn/";
preg_match_all($zengze,$response,$cookie_arr);
//存在匹配,保存
if(!emptyempty($cookie_arr[1])){
$cookiestr=implode(';',$cookie_arr[1]);
file_put_contents($cookiefile,$cookiestr);
echo '成功保存cookie<br>';
}
else echo '沒有匹配到cookie<br>';
}//end if($cookiefile!='')
//返回源碼
return $yuanma;
}//end function request($url,$referer,$postdata,$cookie,$cookiefile)
//獲得當(dāng)前的腳本網(wǎng)址
function GetCurUrl()
{
if(!emptyempty($_SERVER["REQUEST_URI"]))
{
$scriptName = $_SERVER["REQUEST_URI"];
$nowurl = $scriptName;
}
else
{
$scriptName = $_SERVER["PHP_SELF"];
if(emptyempty($_SERVER["QUERY_STRING"]))
{
$nowurl = $scriptName;
}
else
{
$nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
}
}
return $nowurl;
}
//獲得當(dāng)前文件名
$nowurl=GetCurUrl();
//echo $nowurl;
//表單輸出,沒有提交時(shí)
if(!isset($_POST['qq'])){
echo '<form method="post" action="'.$nowurl.'">
qq號(hào)碼:<input type="text" name="qq"><br>
g_tk:<input type="text" name="g_tk"><br>
標(biāo)題:<input type="text" name="title"><br>
內(nèi)容:<input type="text" name="content"><br>
<input type="submit" value="發(fā)表文章">
</form>';
die();
}
/*
提交參數(shù)說(shuō)明:
$_POST['qq']---用戶QQ
$_POST['g_tk']--這個(gè)參數(shù)很關(guān)鍵,獲得這個(gè)參數(shù),需要抓下發(fā)表時(shí)提交的post地址后面調(diào)用的g_tk=123456789,
路POST http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=123456789里的g_tk=123456789
$_POST['title']---文章標(biāo)題,不得空
$_POST['content']---文章內(nèi)容,不得空
*/
header('Content-Type:text/html;charset=gb2312');
set_time_limit(0);
//ob_end_clean();
//ob_start();
//獲取cookie文件,不存在創(chuàng)建,并退出程序
$cookiefile=dirname(__FILE__).'\qq_cookie.txt';
if(!file_exists($cookiefile)){
echo 'qq_cookie.txt不存在,自動(dòng)創(chuàng)建,請(qǐng)?zhí)顚懽グ腸ookie<br>';
file_put_contents($cookiefile,'');
die('程序退出');
}
//存在,讀取cookie
else{
$cookie=file_get_contents($cookiefile);//登錄cookie
//$cookie=urlencode($cookie);
}
//echo 'cookie:'.$cookie.'<br>';
//構(gòu)成發(fā)表頁(yè),post數(shù)據(jù)等的重要信息
//qq號(hào)碼
if(emptyempty($_POST['qq'])||preg_match('/[^0-9]/is',$_POST['qq']))die('qq號(hào)碼有誤,必須數(shù)字');
else $qq=$_POST['qq'];//qq號(hào)
if(emptyempty($_POST['g_tk'])||preg_match('/[^0-9]/is',$_POST['g_tk']))die('post重要參數(shù)g_tk不合法,必須數(shù)字,請(qǐng)使用抓包的值');
$g_tk=$_POST['g_tk'];
$title=emptyempty($_POST['title'])?die('標(biāo)題不得空'):$_POST['title'];//文章標(biāo)題
$content=emptyempty($_POST['content'])?die('內(nèi)容不得空'):$_POST['content'];//內(nèi)容
$category='個(gè)人日記';//分類
$fabiao='http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk='.$g_tk;//發(fā)表處理頁(yè)
$referer='http://ctc.qzs.qq.com/qzone/v5/toolpages/fp_gbk.html';//來(lái)源頁(yè)
$r1='http://user.qzone.qq.com/'.$qq.'/infocenter';//列表訪問(wèn)來(lái)源頁(yè)
$postdata='uin='.$qq.'&category='.urlencode($category).'&title='.urlencode($title).'&content='.urlencode($content).'&html='.urlencode('<div class="blog_details_20110920">'.$content.'</div>').'&tweetflag=0&cb_autograph=1&topflag=0&needfeed=0&g_tk='.$g_tk.'&_fp_refer=http%3A%2F%2Fctc.qzs.qq.com%2Fqzone%2Fnewblog%2Fv5%2Feditor.html%3Fsource%3D1%7Chttp%3A%2F%2Fctc.qzs.qq.com%2Fqzone%2Fnewblog%2Fv5%2Feditor.html%3Fsource%3D1%3Chttp%3A%2F%2Fuser.qzone.qq.com%2F'.$qq.'%2Fmain';//post數(shù)據(jù)
//$postdata=urlencode($postdata);
//echo $postdata;
//發(fā)送請(qǐng)求,獲取源碼
$yuanma=request($fabiao,$r1,$postdata,$cookie,'');
if(strpos($yuanma,'發(fā)表成功'))echo $title.' 發(fā)表成功<br>';
else echo '發(fā)表失敗:右鍵查看源碼,可以看到具體錯(cuò)誤'.$yuanma;
?>
php腳本:注意需要保存命名隨意已經(jīng)自動(dòng)識(shí)別,我是命名為qq_fabiao.php,然后設(shè)置提交地址,cookie文件qq_cookie.txt需要填寫抓包獲取的空間登錄cookie,以通過(guò)登錄驗(yàn)證,qq_cookie.txt與php文件同目錄.
提交參數(shù)說(shuō)明:
$_POST['qq']---用戶QQ
$_POST['g_tk']--這個(gè)參數(shù)很關(guān)鍵,獲得這個(gè)參數(shù),需要抓下發(fā)表時(shí)提交的post地址后面調(diào)用的g_tk=123456789,POST http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=123456789里的g_tk=123456789
$_POST['title']---文章標(biāo)題,不得空
$_POST['content']---文章內(nèi)容,不得空
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
php采用curl實(shí)現(xiàn)偽造IP來(lái)源的方法
這篇文章主要介紹了php采用curl實(shí)現(xiàn)偽造IP來(lái)源的方法,主要涉及使用curl的CURLOPT_REFERER參數(shù)實(shí)現(xiàn)該功能,需要的朋友可以參考下2014-11-11
windows下配置apache+php+mysql時(shí)出現(xiàn)問(wèn)題的處理方法
windows下配置apache+php+mysql應(yīng)該是每個(gè)phper必須掌握的基礎(chǔ)技能了,這也是熟悉php的一個(gè)過(guò)程,小編當(dāng)年自己配環(huán)境的時(shí)候也遇到過(guò)這樣那樣的問(wèn)題,現(xiàn)在把當(dāng)時(shí)記錄的幾個(gè)問(wèn)題的處理方法分享給大家2014-06-06
PHP錯(cuò)誤提示It is not safe to rely on the system……的解決方法
今天小編就為大家分享一篇關(guān)于PHP錯(cuò)誤提示It is not safe to rely on the system……的解決方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
php自定義函數(shù)實(shí)現(xiàn)統(tǒng)計(jì)中文字符串長(zhǎng)度的方法小結(jié)
這篇文章主要介紹了php自定義函數(shù)實(shí)現(xiàn)統(tǒng)計(jì)中文字符串長(zhǎng)度的方法,結(jié)合實(shí)例形式總結(jié)分析了php針對(duì)中文的判定、編碼與運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
Thinkphp中import的幾個(gè)用法詳細(xì)介紹
這篇文章主要介紹了Thinkphp中import的幾個(gè)用法,需要的朋友可以參考下2014-07-07
用PHP控制用戶的瀏覽器--ob*函數(shù)的使用說(shuō)明
用PHP控制用戶的瀏覽器--ob*函數(shù)的使用說(shuō)明...2007-03-03
PHP實(shí)現(xiàn)多圖上傳(結(jié)合uploadify插件)思路分析
這篇文章主要介紹了PHP實(shí)現(xiàn)多圖上傳的方法,實(shí)例分析了php結(jié)合uploadify插件實(shí)現(xiàn)多圖上傳的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-11-11

