Backend Development 4 min read

Using PHP get_headers() to Retrieve HTTP Response Headers

This article explains the PHP get_headers() function, its syntax and parameters, provides code examples for fetching and printing HTTP response headers, and discusses common use cases such as checking remote file existence, obtaining file metadata, and supporting web crawlers.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP get_headers() to Retrieve HTTP Response Headers

Overview: In PHP development we often need to obtain the response header information of a web page or remote resource. The PHP function get_headers() can conveniently retrieve the response headers of a target URL and return them as an array. This article introduces the usage of get_headers() and provides related code examples.

Usage: The get_headers() function can fetch the response headers of a specified URL and return them as an array. The basic syntax is:

array get_headers(string $url, int $format = 0)

Parameters:

$url : the target URL.

$format (optional): determines the format of the returned array. The default value 0 returns an associative array with indexes and values; setting it to 1 returns a simple indexed array.

Code example:

$url = "https://www.example.com";

$headers = get_headers($url);

// Print all response headers
print_r($headers);

// Print specific response headers
echo $headers[0]; // first header
echo $headers[1]; // second header

Output example:

Array (
    [0] => HTTP/1.1 200 OK
    [1] => Date: Thu, 19 Nov 2020 08:00:00 GMT
    [2] => Server: Apache/2.4.41
    [3] => Content-Type: text/html; charset=UTF-8
    [4] => Content-Length: 12345
    ...
)

Application scenarios:

Obtaining remote file information: By fetching the response headers, you can get file size, MIME type, etc.

Checking if a remote file exists: Use the HTTP status code in the headers to determine existence or validity.

Web crawling and network monitoring: Retrieve headers first to decide status codes or other key information before further processing.

Note: The get_headers() function generally only works with the HTTP protocol and is not applicable to other protocols such as FTP.

Summary

The get_headers() function is a very useful PHP function that can easily obtain the response header information of a target URL. By mastering and flexibly applying this function, developers can retrieve HTTP status codes, dates, server details, file sizes, and other header data, improving code usability and efficiency in real‑world projects.

backendHTTPPHPphp-functionsresponse headersget_headers
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.