PHP http_get_last_response_headers() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP 8.4+, http_get_last_response_headers() returns the HTTP response headers from the last request made with PHP's HTTP wrapper, or null if none are stored.

It is the modern alternative to reading the special $http_response_header variable after file_get_contents() or similar HTTP wrapper calls. The value is available outside the local scope of the request call.

Syntax

Call http_get_last_response_headers() with the values you need. Start with this line.

PHP
$headers = http_get_last_response_headers();
CallWhat it does
http_get_last_response_headers()Returns an array of header lines, or null.
http_get_last_response_headers()[0] ?? nullReads the status line when headers exist.
http_clear_last_response_headers()Clears the store so the next get can return null.

Available since PHP 8.4. Headers come from HTTP wrapper transfers (http:// and https:// streams), not from your own header() response to a browser. Failed DNS and similar hard failures may leave null.

Library declarations
PHP
http_get_last_response_headers(): ?array

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
array|nullAn indexed array of response header strings, or null when none are stored.

PHP Examples

Example 1 — Read Headers After an HTTP Wrapper Call

Show the PHP 8.4 getter shape. The printed lines are a typical sample after fetching https://example.com.

PHP
<?php
if (!function_exists("http_get_last_response_headers")) {
    echo "needs PHP 8.4+", PHP_EOL;
    exit;
}

// file_get_contents("https://example.com");
// $headers = http_get_last_response_headers();
$headers = [
    "HTTP/1.1 200 OK",
    "Content-Type: text/html; charset=UTF-8",
];

foreach ($headers as $line) {
    echo $line, PHP_EOL;
}

How It Works

  1. A real script would call file_get_contents() on an http(s) URL first.
  2. http_get_last_response_headers() would then return the response header lines.
  3. This demo prints a fixed sample of those lines for View Output.
  4. Index 0 is usually the HTTP status line.

Example 2 — Null When Nothing Was Fetched

Call the getter with no prior HTTP wrapper request and show the null result.

PHP
<?php
if (!function_exists("http_get_last_response_headers")) {
    echo "needs PHP 8.4+", PHP_EOL;
    exit;
}

http_clear_last_response_headers();
var_dump(http_get_last_response_headers());

How It Works

  1. We clear any previous store first.
  2. No new HTTP wrapper request is made.
  3. The getter returns null.
  4. var_dump() prints NULL.

Example 3 — Pick Out Content-Type

Scan the last response headers for a Content-Type line, using a fixed sample list.

PHP
<?php
$headers = [
    "HTTP/1.1 200 OK",
    "Cache-Control: max-age=60",
    "Content-Type: application/json",
];

$contentType = null;
foreach ($headers as $line) {
    if (stripos($line, "Content-Type:") === 0) {
        $contentType = trim(substr($line, strlen("Content-Type:")));
        break;
    }
}

echo $contentType, PHP_EOL;

How It Works

  1. The sample array matches what http_get_last_response_headers() might return.
  2. We look for a line that starts with Content-Type:.
  3. substr() keeps only the header value.
  4. In PHP 8. 4+ you would fill $headers from the real getter after an HTTP wrapper call.

Frequently Asked Questions

On PHP 8.4+ it returns the response headers from the last HTTP wrapper request as an array, or null if none are stored.
headers_list() shows headers your script is sending to the browser. http_get_last_response_headers() shows headers received from a remote HTTP wrapper request.
That older variable is filled in local scope after an HTTP wrapper call. The new function is easier to use across scopes and for static analysis.
Yes. An HTTP error response still has headers, so the function can return them. Hard transport failures may return null.
PHP 8.4. Check function_exists() if your code must run on older versions.

Next: http_response_code()

http_response_code() gets or sets the HTTP response status code for the current request, s.

http_response_code() tutorial →

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