首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 透過 Cookie 獲取遠程網頁內容並提取指定代碼

     編輯:PHP 相關知識

模擬瀏覽器行為,攜帶 Cookie 進行遠程請求並提取所需片段,適用於需要登入態的頁面採集。

核心功能:Cookie 管理的 cURL 封裝

<?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>";

進階:多帳號 Cookie 隔離管理

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() 處理亂碼
  • Cookie 安全:Cookie 檔案含敏感資訊,設定適當權限 (0600)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved