php通過curl模擬登陸DZ論壇

2022-10-06 00:57:08 字數 2788 閱讀 7648

libcurl同時也支援https認證、http post、ht put、 ftp 上傳(這個也能通過php的ftp擴充套件完成)、http 基於表單的上傳、**、cookies和使用者名稱+密碼的認證。

<?php $discuz_url = '';//論壇位址

$login_url = $discuz_url .'login.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('//i', $contents, $matches);

if(!empty($matches))

//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('/', $contents, $matches);

if(!empty($matches)) else

$post_data = array();

//帖子標題

$post_data['subject'] = 'test2';

//帖子內容

$post_data['message'] = 'test2';

$post_data['topicsubmit'] = "yes";

$post_data['extra'] = '';

//帖子標籤

$post_data['tags'] = 'test';

//帖子的hash碼,這個非常關鍵!假如缺少這個h程式設計客棧ash碼,discuz會警告你來路的頁面不正確

$post_data['formhash']=$formhash;

$c程式設計客棧h = 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);

?>

本文標題: php通過curl模擬登陸dz論壇

本文位址:

CURL模擬登陸

created by phpstorm.user machenike date 2016 7 8 time 13 40 header content type text html charset utf 8 set time limit 0 關閉請求時間 cookie tempnam cookie ...

curl模擬登陸

稍微有點水平的一看就能明白我講的是啥吧.有的內容略.這個東西不能留下能直接用的.培養大家的開發能力.去掉了關鍵注釋 name test.php date thu jan 24 00 24 20 cst 2008 author 馬永佔 myz link header content type text...

Shell 利用 curl 模擬登陸

b 引數 指定使用cookie檔案 c是往cookie檔案中寫cookie d 是指定此次登入所需的引數,通過httpfox檢視 l 指定頁面自動跳轉 curl c ck.txt user agent mozilla 4.0 d username password server id 1 submi...