模擬登入是爬蟲、API 整合、自動化測試的核心技能。本文以模擬登入某論壇 (biuman.com) 為實戰案例,完整展示 Cookie 持久化、CSRF Token 處理、多步驟表單提交、HTTP Header 偽裝、錯誤重試 等企業級爬蟲必備技術。
class LoginSimulator {
private string $cookieFile;
private string $userAgent;
private array $defaultHeaders;
private CurlHandle $ch;
public function __construct(string $cookieDir = __DIR__ . '/cookies') {
$this->cookieFile = $cookieDir . '/cookie_' . md5($_SERVER['HTTP_HOST'] ?? 'default') . '.txt';
$this->userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
$this->defaultHeaders = [
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' => 'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7',
'Accept-Encoding' => 'gzip, deflate, br',
'Connection' => 'keep-alive',
'Upgrade-Insecure-Requests' => '1',
];
if (!is_dir($cookieDir)) mkdir($cookieDir, 0755, true);
}
private function initCurl(): CurlHandle {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_AUTOREFERER => true,
CURLOPT_COOKIEJAR => $this->cookieFile, // 回應 Cookie 寫入檔案
CURLOPT_COOKIEFILE => $this->cookieFile, // 請求攜帶 Cookie
CURLOPT_USERAGENT => $this->userAgent,
CURLOPT_HTTPHEADER => array_map(fn($k, $v) => "$k: $v", array_keys($this->defaultHeaders), $this->defaultHeaders),
CURLOPT_SSL_VERIFYPEER => true, // 生產必須開啟
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_TIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT => 10,
]);
return $ch;
}
// ...
}
public function fetchLoginPage(string $loginUrl): string {
$ch = $this->initCurl();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_HTTPGET, true);
$html = curl_exec($ch);
if (curl_errno($ch)) throw new RuntimeException('Fetch login page failed: ' . curl_error($ch));
curl_close($ch);
return $html;
}
CURLOPT_COOKIEJAR 自動儲存 Set-Cookie 標頭至檔案private function extractFormData(string $html, array $extraFields = []): array {
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$formData = [];
// 提取所有 input[name] 欄位
foreach ($xpath->query('//form//input[@name]') as $input) {
$name = $input->getAttribute('name');
$value = $input->getAttribute('value');
$type = $input->getAttribute('type') ?? 'text';
if ($type !== 'submit' && $type !== 'image') {
$formData[$name] = $value;
}
}
// 合併額外欄位 (帳號、密碼、驗證碼等)
return array_merge($formData, $extraFields);
}
public function submitLogin(string $loginActionUrl, array $postData): string {
$ch = $this->initCurl();
curl_setopt_array($ch, [
CURLOPT_URL => $loginActionUrl,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($postData), // 自動 application/x-www-form-urlencoded
CURLOPT_REFERER => $loginActionUrl, // 關鍵:某些站檢查 Referer
]);
$response = curl_exec($ch);
if (curl_errno($ch)) throw new RuntimeException('Login submit failed: ' . curl_error($ch));
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200 && $httpCode !== 302) {
throw new RuntimeException("Unexpected HTTP status: $httpCode");
}
return $response;
}
public function accessProtectedPage(string $url): string {
$ch = $this->initCurl();
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($ch);
if (curl_errno($ch)) throw new RuntimeException('Access failed: ' . curl_error($ch));
curl_close($ch);
return $html;
}
$simulator = new LoginSimulator();
// 步驟 1: 獲取登入頁面、Cookie、CSRF Token
$loginPageHtml = $simulator->fetchLoginPage('https://www.biuman.com/auth');
$formData = $simulator->extractFormData($loginPageHtml, [
'username' => 'your_account',
'password' => 'your_password',
'captcha' => 'MANUAL_INPUT_OR_OCR_RESULT', // 驗證碼需人工或 OCR 識別
]);
// 步驟 2: 提交登入
try {
$loginResult = $simulator->submitLogin('https://www.biuman.com/home', $formData);
// 判斷登入成功:檢查是否有登出連結、用戶名、導向目標頁
if (strpos($loginResult, 'logout') !== false || strpos($loginResult, 'your_account') !== false) {
echo "✅ 登入成功\n";
// 步驟 3: 訪問需登入的頁面
$profileHtml = $simulator->accessProtectedPage('https://www.biuman.com/user/profile');
// 解析所需資料...
} else {
echo "❌ 登入失敗,回應內容:\n$loginResult";
}
} catch (Exception $e) {
echo "💥 異常:{$e->getMessage()}\n";
}
| 防禦機制 | 繞過方案 |
|---|---|
| User-Agent 檢查 | 使用真實瀏覽器 UA,定期輪換 |
| IP 限流/封禁 | 代理 IP 池 (住宅代理 > 資料中心代理)、請求間隔隨機化 |
| JS 渲染/動態 Token | Headless Chrome (Puppeteer/Playwright) 執行 JS 獲取 Token |
| 瀏覽器指紋識別 | Stealth 插件、模擬 Canvas/WebGL/音訊指紋 |
| 行為分析 (鼠標軌跡、點擊節奏) | 模擬人類行為模型、隨機延遲、錯誤重試 |
CURLOPT_COOKIEJAR/COOKIEFILE)cookies:{domain}:{account_id},TTL 同 Session 過期時間curl_share_init() + CURLSHOPT_SHARE 實現多 cURL handle 共享 Cookie 記憶體 (高併發效能最佳)private function executeWithRetry(callable $curlExec, int $maxRetries = 3, int $baseDelay = 1): mixed {
for ($attempt = 1; $attempt <= $maxRetries; $attempt++) {
try {
return $curlExec();
} catch (RuntimeException $e) {
if ($attempt === $maxRetries) throw $e;
$delay = $baseDelay * (2 ** ($attempt - 1)) + random_int(0, 1000) / 1000; // 指數退避 + 抖動
error_log("Attempt $attempt failed: {$e->getMessage()}, retry in ${delay}s");
usleep($delay * 1_000_000);
}
}
}
cURL 模擬登入的核心在於 完整的 HTTP 狀態管理 (Cookie、Session、Token、Header) 與 對目標站點反爬策略的精準應對。本文提供的類別架構具備生產級的錯誤處理、重試、日誌記錄能力,可直接擴展為通用的爬蟲基礎設施。記住:合法合規、尊重 robots.txt、控制請求頻率 是工程師的底線。