我們這裡是一個簡單的利用php來類比登入後再到QQ空間發送文章的一個簡單的程式,有需要的朋友可以參考
- <?php
-
- //類比get post請求函數 http://www.lai18.com
-
- /*
-
- 函數說明:
-
- 功能:請求方式可以get,post,可以發送的cookie,儲存的cookiefile檔案
-
- 參數:$url-----請求url $referer---來源url $postdata----------用於post請求的資料,''為get請求
-
- $cookie---------發送的cookie $cookiefile-----儲存的cookiefile檔案
-
- 傳回值:返回擷取的源碼
-
- */
-
- function request($url,$referer='',$postdata='',$cookie='',$cookiefile=''){
-
- //header設定
-
- $header='';
-
- $header.="Content-Type: application/x-www-form-urlencodedrn";//內容請求類型
-
- $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";//設定來源地址
-
- $header .= "Cookie:".$cookie ; //設定cookie,預設空
-
- //要求方法get post,通過$postdata空---get,非空----post
-
- if($postdata=='')$method='GET';
-
- else $method='POST';
-
- //定義用於建立流的數組
-
- $opts=array();
-
- $opts['http']=array('method'=>$method,'header'=>$header,'content'=>$postdata);
-
- //產生流
-
- $context=stream_context_create($opts);
-
- //發送請求,擷取源碼
-
- $yuanma=file_get_contents($url,false,$context);
-
- //是否需要儲存cookie到檔案,$cookiefile不空時
-
- if($cookiefile!=''){
-
- echo '需要儲存cookie<br>';
-
- //判斷儲存檔案存在,不存在建立
-
- 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)
-
- //獲得當前的指令碼網址
-
- 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;
-
- }
-
-
-
- //獲得當前檔案名稱
-
- $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>
-
- 標題:<input type="text" name="title"><br>
-
- 內容:<input type="text" name="content"><br>
-
- <input type="submit" value="發表文章">
-
- </form>';
-
- die();
-
- }
-
-
-
- /*
-
- 提交參數說明:
-
- $_POST['qq']---使用者QQ
-
- $_POST['g_tk']--這個參數很關鍵,獲得這個參數,需要抓下發表時提交的post地址後面調用的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']---文章標題,不得空
-
- $_POST['content']---文章內容,不得空
-
- */
-
-
-
- header('Content-Type:text/html;charset=gb2312');
-
- set_time_limit(0);
-
- //ob_end_clean();
-
- //ob_start();
-
-
-
- //擷取cookie檔案,不存在建立,並退出程式
-
- $cookiefile=dirname(__FILE__).'\qq_cookie.txt';
-
- if(!file_exists($cookiefile)){
-
- echo 'qq_cookie.txt不存在,自動建立,請填寫抓包的cookie<br>';
-
- file_put_contents($cookiefile,'');
-
- die('程式退出');
-
- }
-
- //存在,讀取cookie
-
- else{
-
- $cookie=file_get_contents($cookiefile);//登入cookie
-
- //$cookie=urlencode($cookie);
-
- }
-
- //echo 'cookie:'.$cookie.'<br>';
-
-
-
- //構成發表頁,post資料等的重要訊息
-
- //qq號碼
-
- if(emptyempty($_POST['qq'])preg_match('/[^0-9]/is',$_POST['qq']))die('qq號碼有誤,必須數字');
-
- else $qq=$_POST['qq'];//qq號
-
- if(emptyempty($_POST['g_tk'])preg_match('/[^0-9]/is',$_POST['g_tk']))die('post重要參數g_tk不合法,必須數字,請使用抓包的值');
-
- $g_tk=$_POST['g_tk'];
-
-
-
- $title=emptyempty($_POST['title'])?die('標題不得空'):$_POST['title'];//文章標題
-
- $content=emptyempty($_POST['content'])?die('內容不得空'):$_POST['content'];//內容
-
-
-
- $category='個人日記';//分類
-
- $fabiao='http://b1.qzone.qq.com/cgi-bin/blognew/blog_add?g_tk='.$g_tk;//發表處理頁
-
- $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資料
-
- //$postdata=urlencode($postdata);
-
- //echo $postdata;
-
- //發送請求,擷取源碼
-
- $yuanma=request($fabiao,$r1,$postdata,$cookie,'');
-
- if(strpos($yuanma,'發表成功'))echo $title.' 發表成功<br>';
-
- else echo '發表失敗:右鍵查看源碼,可以看到具體錯誤'.$yuanma;
-
- ?>