一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

php中通過curl模擬登陸discuz論壇的實現代碼

libcurl同時也支持HTTPS認證、HTTP POST、HTTP PUT、 FTP 上傳(這個也能通過php的FTP擴展完成)、HTTP 基于表單的上傳、代理、cookies和用戶名+密碼的認證。
php的curl真的是相當好用,網上一搜索相關文章都是關于curl模擬登陸的,很少人提供模擬discuz發貼的源碼。
復制代碼 代碼如下:
<?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('/<input/s*type="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_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('/<input/s*type="hidden"/s*name="formhash"/s*id="formhash"/s*value="(.*?)"/s*//>/i', $contents, $matches);
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, 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技術php中通過curl模擬登陸discuz論壇的實現代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产男人午夜视频在线观看 | 亚洲一区二区三 | 久久久噜噜噜久久 | 国产精品一区二区在线观看 | 亚洲国产第一 | 美女扒开腿让男人捅爽 | 国产高清一级毛片在线人 | 91精品国产丝袜高跟鞋 | 日本美女视频韩国视频网站免费 | 亚洲激情网址 | 色婷婷激情五月 | 免费一级乱子伦片 | 无码精品一区二区三区免费视频 | 久久国产香蕉 | 国产在线精品国自产拍影院同性 | 夜色资源站www国产在线观看 | 国内精品久久久久影院网站 | 免费观看欧美成人1314w色 | 亚洲狠狠婷婷综合久久久久网站 | 免费的成人a视频在线观看 免费的黄色片视频 | 亚洲激情在线观看 | 久久精品影院永久网址 | 四虎澳门永久8848在线影院 | 综合久久一区二区三区 | 欧美一a一片一级一片 | 久久国产精品99久久久久久牛牛 | 精品一区二区三区免费站 | 亚洲成人欧美 | 成人国产在线24小时播放视频 | 999免费视频| 亚洲一区二区免费视频 | 美女激情网 | 四虎海外影库www4hu | 丁香六月 久久久 | 女人与拘一级毛片 | 五月天激情综合网 | 久久国产精品视频 | 久久精品观看 | 国产日韩精品一区在线不卡 | 亚洲精品久 | 国产亚洲精品hd网站 |