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

file_get_contents 失效解決方案:改用 cURL 取得網頁內容

     編輯:PHP 綜合知識

file_get_contents 無法取得遠端內容的常見原因與替代方案

file_get_contents() 依賴 allow_url_fopen=On,且不支援 HTTPS 驗證、POST、Cookie、Header 自訂等進階功能。生產環境建議統一使用 cURL。

cURL 替代範例(支援逾時、HTTPS、自訂 Header)

<?php
function fetchUrl($url, $options = []) {
    $ch = curl_init($url);
    curl_setopt_array($ch, array_merge([
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CONNECTTIMEOUT => 5,
        CURLOPT_TIMEOUT => 10,
        CURLOPT_SSL_VERIFYPEER => true,  // 生產環境建議 true
        CURLOPT_FOLLOWLOCATION => true,  // 自動跟隨 3xx
        CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; PHP Bot)',
    ], $options));

    $content = curl_exec($ch);
    $errno = curl_errno($ch);
    $error = curl_error($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);

    if ($errno) {
        throw new Exception("cURL Error [$errno]: $error");
    }
    if ($info['http_code'] >= 400) {
        throw new Exception("HTTP {$info['http_code']}: $url");
    }
    return $content;
}

// 使用範例
try {
    $html = fetchUrl('https://www.example.com/');
    echo $html;
} catch (Exception $e) {
    echo '抓取失敗: ' . $e->getMessage();
}
?>

常見 file_get_contents 失效原因

原因說明解決
allow_url_fopen=Offphp.ini 關閉遠端開啟改用 cURL 或開啟設定(不建議)
HTTPS 憑證驗證失敗無 CA 憑證或自簽憑證cURL 設定 CURLOPT_CAINFO 或暫時關閉驗證
目標網站封鎖 PHP User-Agent預設 User-Agent 為 "PHP/版本"cURL 自訂 CURLOPT_USERAGENT
需求 POST / Cookie / Headerfile_get_contents 僅支援 GETcURL 完整支援
連線逾時無法控制受 default_socket_timeout 限制cURL 可精確設定連線/讀取逾時

進階:支援 Basic Auth 的抓取

<?php
$url = 'https://api.example.com/protected';
$options = [
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => 'username:password',
];
$content = fetchUrl($url, $options);
?>

封裝成可重用的 HttpClient 類別,統一處理錯誤、重試、日誌、快取,是現代 PHP 專案的標準做法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved