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
广告

© . All rights reserved.