
Definition
In PHP, get_headers() asks a web server for a URL and returns the response headers, such as the status line and Content-Type.
It needs network access and allow_url_fopen turned on, so the sample output on this page is typical, not exact.
Syntax
Call get_headers() with the values you need. Start with this line.
$headers = get_headers("https://example.com");
| Call | What it does |
|---|
get_headers("https://example.com") | Returns a list of header lines. |
get_headers("https://example.com", true) | Returns headers keyed by name. |
The first item is the status line, such as HTTP/1.1 200 OK. Real values depend on the server you ask.
Library declarations
get_headers(string $url, bool $associative = false, ?resource $context = null): array|false
Parameters
| Parameter | Description |
|---|
| url | Required. The target URL. |
| associative | Optional. If true, the result uses header names as keys. Default is false. |
| context | Optional. A stream context, for example to change the HTTP method or add a timeout. |
Return Value
| Return Value | Description |
|---|
| array|false | An array of headers, or false on failure. |
PHP Examples
Example 1 — Read the Headers of a URL
Ask a server for its headers. The output below is a typical sample, because real headers depend on the server.
<?php
$headers = get_headers("https://example.com");
print_r($headers);
Array
(
[0] => HTTP/1.1 200 OK
[1] => Content-Type: text/html; charset=UTF-8
[2] => Content-Length: 1256
)How It Works
- Pass a URL. PHP opens a connection to the server.
- Get an array of lines. Index 0 is the status line.
- Read the other headers. Each item looks like
Name: value. - Expect different values. Typical sample output is shown here.
Example 2 — Use Header Names as Keys
Set the second argument to true so you can read a header by its name. The output is a typical sample.
<?php
$headers = get_headers("https://example.com", true);
echo $headers["Content-Type"], PHP_EOL;
How It Works
- Pass true as the second argument. The result becomes an associative array.
- Look up a header by name. The key is
Content-Type. - Handle repeats. A header sent twice becomes an array of values.
- Check for false first. The request can fail when the host is offline.
Frequently Asked Questions
get_headers() requests a URL and returns the HTTP response headers sent by the server.
get_headers(string $url, bool $associative = false, ?resource $context = null): array|false. Pass the URL and, optionally, true for named keys.
It returns false when the request fails, for example when the host is unreachable, the URL is wrong, or allow_url_fopen is disabled.
Use a stream context with the HEAD method: stream_context_create(['http' => ['method' => 'HEAD']]) and pass it as the third argument.
Yes. When the server redirects, the result contains the headers of each step, so the array may hold more than one status line.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful