php怎么通過curl模擬登陸DZ論壇-創(chuàng)新互聯(lián)

本篇內容主要講解“php怎么通過curl模擬登陸DZ論壇”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“php怎么通過curl模擬登陸DZ論壇”吧!

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網綜合服務,包含不限于成都網站制作、成都網站建設、武宣網絡推廣、微信小程序定制開發(fā)、武宣網絡營銷、武宣企業(yè)策劃、武宣品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學生創(chuàng)業(yè)者提供武宣建站搭建服務,24小時服務熱線:18980820575,官方網址:muchs.cn

libcurl同時也支持HTTPS認證、HTTP POST、HTTP PUT、 FTP 上傳(這個也能通過PHP的FTP擴展完成)、HTTP 基于表單的上傳、代理、cookies和用戶名+密碼的認證。

<?php 
$discuz_url = '/tupian/20230522/&
$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('/<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文件去模擬發(fā)帖,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怎么通過curl模擬登陸DZ論壇”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

網頁題目:php怎么通過curl模擬登陸DZ論壇-創(chuàng)新互聯(lián)
網頁URL:http://muchs.cn/article16/deijgg.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供用戶體驗、網站策劃、網站改版Google、小程序開發(fā)、外貿建站

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網頁設計公司