PHP - get_headers() 函式



PHP URL 的 get_headers() 函式用於獲取伺服器響應 HTTP 請求時傳送的所有標頭。它返回一個包含伺服器響應 HTTP 請求時傳送的標頭的陣列。

語法

以下是 PHP URL 的 get_headers() 函式的語法:

array get_headers(string $url, int $format = 0, ?resource $context = null)

引數

以下是 get_headers() 函式的引數:

  • $url - 要從中獲取標頭的 URL。

  • $format - 指定如何索引標頭。其預設值為 0。

  • $context - 使用 stream_context_create() 建立的有效上下文資源。

返回值

get_headers() 函式返回一個標頭陣列。如果失敗則返回 FALSE。

PHP 版本

get_headers() 函式首次引入核心 PHP 5,並在 PHP 7 和 PHP 8 中繼續輕鬆執行。

示例 1

首先,我們將向您展示 PHP URL get_headers() 函式的基本示例,以從給定 URL 獲取標頭。

<?php
   // Define the url here
   $url = "http://www.example.com";
   $headers = get_headers($url);
   print_r($headers);
?>

輸出

以上程式碼將產生類似以下的結果:

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Accept-Ranges: bytes
    [2] => Age: 482592
    [3] => Cache-Control: max-age=604800
    [4] => Content-Type: text/html; charset=UTF-8
    [5] => Date: Wed, 24 Jul 2024 08:45:42 GMT
    [6] => Etag: "3147526947"
    [7] => Expires: Wed, 31 Jul 2024 08:45:42 GMT
    [8] => Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
    [9] => Server: ECAcc (dcd/7D43)
    [10] => Vary: Accept-Encoding
    [11] => X-Cache: HIT
    [12] => Content-Length: 1256
    [13] => Connection: close
)

示例 2

現在我們將嘗試使用 get_headers() 函式獲取標頭並將其結果作為關聯陣列返回。

<?php
   /// Define url here
   $url = "http://www.tutorix.com";
   $headers = get_headers($url, 1);
   print_r($headers);
?> 

輸出

這將生成以下輸出:

Array
(
    [0] => HTTP/1.1 302 Moved Temporarily
    [Content-Type] => Array
        (
            [0] => text/html; charset=iso-8859-1
            [1] => text/html; charset=UTF-8
        )

    [Content-Length] => 210
    [Connection] => Array
        (
            [0] => close
            [1] => close
        )

    [Date] => Array
        (
            [0] => Wed, 24 Jul 2024 08:46:34 GMT
            [1] => Wed, 24 Jul 2024 08:46:35 GMT
        )

    [Server] => Array
        (
            [0] => Apache
            [1] => Apache
        )
)

示例 3

現在,在以下程式碼中,我們使用 get_headers() 函式獲取標頭。然後,我們將檢查是否存在特定的標頭(例如 Content-Type),並相應地顯示內容。

<?php
   // Define url here
   $url = "http://www.tutorix.com";
   $headers = get_headers($url, 1);
   
   if (isset($headers['Content-Type'])) {
       echo"Content-Type: "; 
       print_r($headers['Content-Type']);
   } else {
       echo "Content-Type header not found.";
   }
?> 

輸出

這將建立以下輸出:

Content-Type: Array
(
    [0] => text/html; charset=iso-8859-1
    [1] => text/html; charset=UTF-8
)

示例 4

在以下示例中,我們使用 get_headers() 函式使用自定義上下文獲取標頭。

<?php
   // Define url here
   $url = "http://www.tutorix.com";
   $options = array(
      "http" => array(
          "method" => "GET",
          "header" => "User-Agent: PHP"
      )
   );
   $context = stream_context_create($options);
   $headers = get_headers($url, 0, $context);
   print_r($headers);
?> 

輸出

以下是以上程式碼的輸出:

Array
(
    [0] => HTTP/1.1 302 Moved Temporarily
    [1] => Content-Type: text/html; charset=iso-8859-1
    [2] => Content-Length: 210
    [3] => Connection: close
    [4] => Date: Wed, 24 Jul 2024 08:46:34 GMT
    [5] => Server: Apache
    [6] => Location: https://origin.tutorix.com
    [7] => X-Cache: Hit from cloudfront
    [8] => Via: 1.1 6c978f94bb9ef67954e101caa67e6c38.cloudfront.net (CloudFront)
    [9] => X-Amz-Cf-Pop: DEL51-P1
    [10] => X-Amz-Cf-Id: UmSPYyvcQMuJqO3qmWr2GJvpyBRa738Sj3cufsVJ5PbiYtP_a5PZCw==
    [11] => Age: 315
    [12] => HTTP/1.1 200 OK
    [13] => Date: Wed, 24 Jul 2024 08:51:50 GMT
    [14] => Server: Apache
    [15] => X-Powered-By: PHP/7.2.22
    [16] => Set-Cookie: TUTORIXSESSIONID=ecso7cina18m2ine1os429hrr8; expires=Thu, 25-Jul-2024 08:51:50 GMT; Max-Age=86400; path=/
    [17] => Vary: Accept-Encoding
    [18] => Access-Control-Allow-Origin: *
    [19] => Cache-Control: max-age=3604800, public
    [20] => Connection: close
    [21] => Transfer-Encoding: chunked
    [22] => Content-Type: text/html; charset=UTF-8
)
php_function_reference.htm
廣告