模擬瀏覽器行為,攜帶 Cookie 進行遠程請求並提取所需片段,適用於需要登入態的頁面採集。
<?php
function steal($url, $postData = '') {
$userAgent = 'Mozilla/4.0';
$followLocation = 1;
$cookieFile = './tmp/cook.txt'; // Cookie 儲存檔案
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_USERAGENT => $userAgent,
CURLOPT_COOKIEJAR => $cookieFile, // 寫入 Cookie
CURLOPT_COOKIEFILE => $cookieFile, // 讀取 Cookie
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => $followLocation,
CURLOPT_TIMEOUT => 1000,
]);
if (trim($postData) !== '') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function cut($content, $startTag, $endTag) {
$parts = explode($startTag, $content);
if (count($parts) < 2) {
return "<font color='#ff0000'>無法獲取內容: 找不到起始標記</font>";
}
$parts = explode($endTag, $parts[1]);
if ($parts[0] === '') {
return "<font color='#ff0000'>無法獲取內容: 截取結果為空</font>";
}
return $parts[0];
}
<?php
// 1. 先訪問登入頁建立 Cookie
$loginUrl = 'https://example.com/login.php';
$loginPost = 'username=test&password=123456';
steal($loginUrl, $loginPost);
// 2. 訪問需登入的目標頁
$targetUrl = 'https://example.com/user/profile.php';
$html = steal($targetUrl);
// 3. 提取所需片段
$userName = cut($html, '<div class="username">', '</div>');
$userEmail = cut($html, '<span id="email">', '</span>');
echo "用戶名: $userName<br>";
echo "郵箱: $userEmail<br>";
class CookieManager {
private $cookieDir;
public function __construct($dir = './cookies/') {
$this->cookieDir = $dir;
if (!is_dir($dir)) mkdir($dir, 0755, true);
}
public function getCookieFile($accountId) {
return $this->cookieDir . 'cookie_' . md5($accountId) . '.txt';
}
public function request($url, $accountId, $postData = '') {
$cookieFile = $this->getCookieFile($accountId);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_COOKIEJAR => $cookieFile,
CURLOPT_COOKIEFILE => $cookieFile,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
]);
if ($postData) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
// 使用
$cm = new CookieManager();
$html1 = $cm->request('https://site.com/page1', 'account_a');
$html2 = $cm->request('https://site.com/page2', 'account_b');
robots.txt 與服務條款sleep() 或 usleep()curl_errno()、HTTP 狀態碼mb_check_encoding() + iconv() 處理亂碼