libcurl同時也支援HTTPS認證、HTTP POST、HTTP PUT、 FTP 上傳(這個也能透過PHP的FTP擴充完成)、HTTP 基於表單的上傳、代理、cookies和使用者名稱+密碼的認證。
php的curl真的是相當好用,網路上一搜尋相關文章都是關於curl模擬登陸的,很少人提供模擬discuz貼的源碼。
複製程式碼 程式碼如下:
$discuz_url = 'http://127.0.0.1/discuz/';//論壇地址
. action=login';//登入頁位址
$post_fields = array();
//以下兩個項目不需要修改
$post_fields['loginfield'] = 'username';
$post_fields['loginsubmit'] = ' true';
//使用者名稱與密碼,必須填入
$post_fields['username'] = 'tianxin';
$post_fields['password'] = '111111';
//安全提問
$post_fields['questid' ] = 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(e(cha); "hidden"s*name="formhash"s*value="(.*?)"s*/>/i', $contents, $matches);
if(!empty($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_RETURNTRANSF, 1ETURNTRANSF, 1. 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);就可以帶著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, $execookie_file);
f fg] url;的hash碼和登陸視窗的hash碼的正規則不太一樣,這裡的hidden多了一個id屬性
preg_match('/
if(!empty($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, CURNTRANSFER, 0);
curl_se頂1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$contents = curl_exec($ch);
curl_close($ch);
//清理cookieter
unlink($cookie_file);
//清理cookie檔
unlink($cookie_file);
以上就介紹了discuz論壇範本 php中透過curl模擬登陸discuz論壇的實作程式碼,包含了discuz論壇範本方面的內容,希望對PHP教學有興趣的朋友有幫助。