要模擬瀏覽器訪問網站,首選要學會觀察瀏覽器是如何發送http報文的,以及網站服務器返回給瀏覽器 是什么樣的內容,我推薦安裝一個國外人開發的httpwatch的軟件,最好搞個破解的版本,否則有些功能是使用不了的,這個軟件安裝完成之后是嵌入在 IE里的,啟動Record,在地址欄輸入網址后回車,它就會將瀏覽器和服務器之間的所有通訊掃描出來,讓你一覽無遺,關于這個軟件的使用在本文不做介紹.
模擬瀏覽器登陸應用開發,最關鍵的地方是突破登陸驗證,CURL技術不只支持http,還支持https,區別就在多了一層SSL加密傳輸,如果是要登陸 https網站,php記得要支持openssl,還是先拿一個例子來分析,代碼如下:
- <?php
- $discuz_url = 'http://127.0.0.1/discuz/'; //論壇地址
- $login_url = $discuz_url . 'logging.php?action=login'; //登錄頁地址
- $post_fields = array();
- //以下兩項不需要修改
- $post_fields['loginfield'] = 'username';
- $post_fields['loginsubmit'] = 'true';
- //用戶名和密碼,必須填寫
- $post_fields['username'] = 'tianxin';
- $post_fields['password'] = '111111';
- //安全提問
- $post_fields['questionid'] = 0;
- $post_fields['answer'] = '';
- //@todo驗證碼
- $post_fields['seccodeverify'] = '';
- //獲取表單FORMHASH
- $ch = curl_init($login_url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $contents = curl_exec($ch);
- curl_close($ch);
- preg_match('/<inputs*type="hidden"s*name="formhash"s*value="(.*?)"s*/>/i', $contents, $matches);
- if (!emptyempty($matches)) {
- $formhash = $matches[1];
- } else {
- die('Not found the forumhash.');
- }
- //POST數據,獲取COOKIE,cookie文件放在網站的temp目錄下
- $cookie_file = tempnam('./temp', 'cookie');
- $ch = curl_init($login_url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
- curl_exec($ch);
- curl_close($ch);
- //取到了關鍵的cookie文件就可以帶著cookie文件去模擬發帖,fid為論壇的欄目ID
- $send_url = $discuz_url . "post.php?action=newthread&fid=2";
- $ch = curl_init($send_url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
- $contents = curl_exec($ch);
- curl_close($ch);
- //這里的hash碼和登陸窗口的hash碼的正則不太一樣,這里的hidden多了一個id屬性
- preg_match('/<inputs*type="hidden"s*name="formhash"s*id="formhash"s*value="(.*?)"s*/>/i', $contents, $matches);
- if (!emptyempty($matches)) {
- $formhash = $matches[1];
- } else {
- die('Not found the forumhash.');
- }
- $post_data = array();
- //帖子標題
- $post_data['subject'] = 'test2';
- //帖子內容
- $post_data['message'] = 'test2';
- $post_data['topicsubmit'] = "yes";
- $post_data['extra'] = '';
- //帖子標簽
- $post_data['tags'] = 'test';
- //帖子的hash碼,這個非常關鍵!假如缺少這個hash碼,discuz會警告你來路的頁面不正確
- $post_data['formhash'] = $formhash;
- $ch = curl_init($send_url);
- curl_setopt($ch, CURLOPT_REFERER, $send_url); //偽裝REFERER
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- $contents = curl_exec($ch);
- curl_close($ch);
- //清理cookie文件
- unlink($cookie_file);
- ?>
CURL實現網站模擬登陸,代碼如下:
- <?php
- $cookie_file=tempnam('./temp','cookie');$login_url='/bbs/logging.php?action=login&loginsubmit=yes';$post_fields='username=用戶名&password=用戶密碼&referer=index.php&formhash=24eca8af&loginfield=username&questionid=0&loginsubmit=登錄';$ch = curl_init($login_url);curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$post_fields);curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);curl_exec($ch);curl_close($ch);$url='/bbs';$ch =curl_init($url);curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file);$contents=curl_exec($ch);echo $contents;curl_close($ch);
- ??>
新聞熱點
疑難解答