PHP get_headers() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
URL

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.

PHP
$headers = get_headers("https://example.com");
CallWhat 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
PHP
get_headers(string $url, bool $associative = false, ?resource $context = null): array|false

Parameters

ParameterDescription
urlRequired. The target URL.
associativeOptional. If true, the result uses header names as keys. Default is false.
contextOptional. A stream context, for example to change the HTTP method or add a timeout.

Return Value

Return ValueDescription
array|falseAn 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
<?php
$headers = get_headers("https://example.com");

print_r($headers);

How It Works

  1. Pass a URL. PHP opens a connection to the server.
  2. Get an array of lines. Index 0 is the status line.
  3. Read the other headers. Each item looks like Name: value.
  4. 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
<?php
$headers = get_headers("https://example.com", true);

echo $headers["Content-Type"], PHP_EOL;

How It Works

  1. Pass true as the second argument. The result becomes an associative array.
  2. Look up a header by name. The key is Content-Type.
  3. Handle repeats. A header sent twice becomes an array of values.
  4. Check for false first. The request can fail when the host is offline.

Example 3 — Read the Status Code (Offline Demo)

This demo works without a network. It uses a fixed list of header lines, the same shape that get_headers() returns.

PHP
<?php
$headers = [
    "HTTP/1.1 200 OK",
    "Content-Type: text/html; charset=UTF-8",
    "Content-Length: 1256",
];

$parts = explode(" ", $headers[0]);

echo "Status code: ", $parts[1], PHP_EOL;

How It Works

  1. Use a fixed sample list. The first line is the status line.
  2. Split it by spaces. explode() gives HTTP/1.1, 200 and OK.
  3. Read the second part. That is the status code.
  4. Use the same idea with real data. Replace the list with get_headers() output.

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.

Next: get_meta_tags()

get_meta_tags() reads the meta tags from a web page or an HTML file.

get_meta_tags() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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