php模擬用戶自動在qq空間發(fā)表文章的方法
我們這里是一個簡單的利用php來模擬登錄后再到QQ空間發(fā)送文章的一個簡單的程序,有需要的朋友可以參考
我們這里是一個簡單的利用php來模擬登錄后再到QQ空間發(fā)送文章的一個簡單的程序,有需要的朋友可以參考
- <?php
- //模擬get post請求函數(shù) http://www.lai18.com
- /*
- 函數(shù)說明:
- 功能:請求方式可以get,post,可以發(fā)送的cookie,保存的cookiefile文件
- 參數(shù):$url-----請求url $referer---來源url $postdata----------用于post請求的數(shù)據(jù),''為get請求
- $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)容請求類型
- $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è)置來源地址
- $header .= "Cookie:".$cookie ; //設(shè)置cookie,默認(rèn)空
- //請求方法get post,通過$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ā)送請求,獲取源碼
- $yuanma=file_get_contents($url,false,$context);
- //是否需要保存cookie到文件,$cookiefile不空時
- if($cookiefile!=''){
- echo '需要保存cookie<br>';
- //判斷保存文件存在,不存在創(chuàng)建
- if(!file_exists($cookiefile)){
- file_put_contents($cookiefile,'');
- }
- //獲取cookie,保存起來
- $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;
- //表單輸出,沒有提交時
- if(!isset($_POST['qq'])){
- echo '<form method="post" action="'.$nowurl.'">
- qq號碼:<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ù)說明:
- $_POST['qq']---用戶QQ
- $_POST['g_tk']--這個參數(shù)很關(guān)鍵,獲得這個參數(shù),需要抓下發(fā)表時提交的post地址后面調(diào)用的g_tk=123456789,
- 路POST <a href="http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=123456789里的g_tk=123456789" target="_blank">http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk=123456789里的g_tk=123456789</a>
- $_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不存在,自動創(chuà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ā)表頁,post數(shù)據(jù)等的重要信息
- //qq號碼
- if(emptyempty($_POST['qq'])||preg_match('/[^0-9]/is',$_POST['qq']))die('qq號碼有誤,必須數(shù)字');
- else $qq=$_POST['qq'];//qq號
- if(emptyempty($_POST['g_tk'])||preg_match('/[^0-9]/is',$_POST['g_tk']))die('post重要參數(shù)g_tk不合法,必須數(shù)字,請使用抓包的值');
- $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='個人日記';//分類
- $fabiao='http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk='.$g_tk;//發(fā)表處理頁
- $referer='http://ctc.qzs.qq.com/qzone/v5/toolpages/fp_gbk.html';//來源頁
- $r1='http://user.qzone.qq.com/'.$qq.'/infocenter';//列表訪問來源頁
- $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ā)送請求,獲取源碼
- $yuanma=request($fabiao,$r1,$postdata,$cookie,'');
- if(strpos($yuanma,'發(fā)表成功'))echo $title.' 發(fā)表成功<br>';
- else echo '發(fā)表失?。河益I查看源碼,可以看到具體錯誤'.$yuanma;
- ?>
責(zé)任編輯:王雪燕
來源:
開源中國社區(qū)