Use get_headers() when you need to retrieves HTTP response headers from a remote URL in a PHP application. The examples below show the actual operation and its limitations.
What Is get_headers() in PHP?
Retrieves HTTP response headers from a remote URL. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
get_headers(string $url, bool $associative = ..., $context = ...): array|false
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
$headers = @get_headers("http://127.0.0.1:1", true); echo $headers === false ? "Unavailable" : "Headers received";
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Example 2: Different Input or Behavior
<?php
$headers = @get_headers("http://127.0.0.1:9"); var_dump(is_array($headers));
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Practical Example: Applying the Function
<?php
$url = "http://127.0.0.1:1"; $context = stream_context_create(["http"=>["timeout"=>1]]); $headers = @get_headers($url,false,$context); echo $headers === false ? "Request failed" : $headers[0];
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Important Behavior and Edge Cases
Remote requests can hang, redirect, or fail; set a timeout and do not fetch arbitrary untrusted URLs.
Comparison With Related Functions
| Approach | Difference |
|---|---|
get_headers() | Retrieves HTTP response headers from a remote URL. |
| Related operation | curl_getinfo() provides transfer metadata for cURL requests; get_headers() retrieves header lines. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Remote requests can hang, redirect, or fail; set a timeout and do not fetch arbitrary untrusted URLs. |
| Confusing this function with a related operation | curl_getinfo() provides transfer metadata for cURL requests; get_headers() retrieves header lines. |
Key Takeaways
- Retrieves HTTP response headers from a remote URL.
- Remote requests can hang, redirect, or fail; set a timeout and do not fetch arbitrary untrusted URLs.
- curl_getinfo() provides transfer metadata for cURL requests; get_headers() retrieves header lines.
Use get_headers() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.