cURL 模擬瀏覽器 HTTP 交互,關鍵在於:
formhash)CURLOPT_COOKIEJAR/FILE)<?php
$discuz_url = 'https://127.0.0.1/discuz/';
$login_url = $discuz_url . 'logging.php?action=login';
// 登錄表單數據
$post_fields = [
'loginfield' => 'username',
'loginsubmit' => 'true',
'username' => 'tianxin', // 修改為真實用戶名
'password' => '111111', // 修改為真實密碼
'questionid' => 0,
'answer' => '',
'seccodeverify' => '', // 驗證碼(如有需額外處理)
];
// 步驟 1:獲取登錄頁面中的 formhash
$ch = curl_init($login_url);
curl_setopt_array($ch, [
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
]);
$contents = curl_exec($ch);
curl_close($ch);
preg_match('/<input[^>]*name="formhash"[^>]*value="([^"]*)"/i', $contents, $matches);
if (empty($matches)) die('Not found the forumhash.');
$formhash = $matches[1];
// 步驟 2:提交登錄,保存 Cookie
$cookie_file = tempnam('./temp', 'cookie');
$post_fields['formhash'] = $formhash;
$ch = curl_init($login_url);
curl_setopt_array($ch, [
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_fields,
CURLOPT_COOKIEJAR => $cookie_file, // 保存 Cookie
]);
curl_exec($ch);
curl_close($ch);
// 步驟 3:攜帶 Cookie 訪問發帖頁,獲取發帖 formhash
$send_url = $discuz_url . "post.php?action=newthread&fid=2";
$ch = curl_init($send_url);
curl_setopt_array($ch, [
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_COOKIEFILE => $cookie_file, // 讀取 Cookie
]);
$contents = curl_exec($ch);
curl_close($ch);
preg_match('/<input[^>]*name="formhash"[^>]*id="formhash"[^>]*value="([^"]*)"/i', $contents, $matches);
if (empty($matches)) die('Not found the forumhash.');
$formhash = $matches[1];
// 步驟 4:提交發帖數據
$post_data = [
'subject' => '測試標題',
'message' => '測試內容',
'topicsubmit' => 'yes',
'extra' => '',
'tags' => '測試,標簽',
'formhash' => $formhash, // 關鍵:防跨站請求偽造
];
$ch = curl_init($send_url);
curl_setopt_array($ch, [
CURLOPT_REFERER => $send_url, // 偽裝來源
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 0, // 直接輸出
CURLOPT_COOKIEFILE => $cookie_file,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_data,
]);
curl_exec($ch);
curl_close($ch);
// 清理臨時 Cookie 文件
unlink($cookie_file);
?>
<?php
$cookie_file = tempnam('./temp', 'cookie');
$login_url = '/bbs/logging.php?action=login&loginsubmit=yes';
$post_fields = http_build_query([
'username' => '用戶名',
'password' => '密碼',
'referer' => 'index.php',
'formhash' => '24eca8af', // 需動態獲取
'loginfield' => 'username',
'questionid' => 0,
'loginsubmit' => '登錄',
]);
$ch = curl_init($login_url);
curl_setopt_array($ch, [
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_fields,
CURLOPT_COOKIEJAR => $cookie_file,
]);
curl_exec($ch);
curl_close($ch);
// 後續請求攜帶 Cookie
$ch = curl_init('/bbs');
curl_setopt_array($ch, [
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_COOKIEFILE => $cookie_file,
]);
$contents = curl_exec($ch);
echo $contents;
curl_close($ch);
?>
| 選項 | 作用 |
|---|---|
| CURLOPT_COOKIEJAR | 響應中保存 Cookie 到文件 |
| CURLOPT_COOKIEFILE | 請求中讀取 Cookie 文件 |
| CURLOPT_FOLLOWLOCATION | 自動跟隨 3xx 重定向 |
| CURLOPT_SSL_VERIFYPEER | 設 0 禁用 SSL 證書驗證(測試用) |
| CURLOPT_USERAGENT | 偽裝瀏覽器標識 |
| CURLOPT_REFERER | 偽裝來源頁面 |
| CURLOPT_POST | 啟用 POST 請求 |
| CURLOPT_POSTFIELDS | POST 數據(陣列或 urlencode 字串) |
CURLOPT_SSL_VERIFYPEER => false (僅測試)