PHP: get_headers set temporary stream_context
Asked Answered
M

4

8

I guess PHP's get_headers does not allow for a context, so I have to change the default stream context to only get the HEAD of a request. This causes some issues with other requests on the page. I can't seem to figure out how to reset the default stream context. I'm trying something like:

$default = stream_context_get_default(); //Get default stream context so we can reset it
stream_context_set_default( //Only fetch the HEAD
      array(
    'http' => array(
       'method' => 'HEAD'
     )
  )
);
$headers = get_headers($url, 1); //Url can be whatever you want it to be
//var_dump($headers);
var_dump($default);
stream_context_set_default($default); //This doesn't work as it expects an array and not a resource pointer

Does anyone know a fix for this?

I know it has been suggested to use Curl, but I would rather not for this one. Thanks!

Maddening answered 8/12, 2011 at 10:16 Comment(1)
get_headers Inconsistency : https://mcmap.net/q/112977/-get_headers-inconsistency-closedTimmy
R
10

I ended up using the stream_get_meta_data() function to get the HTTP headers.

This is how I implemented it:

function get_headers_with_stream_context($url, $context, $assoc = 0) {
    $fp = fopen($url, 'r', null, $context);
    $metaData = stream_get_meta_data($fp);
    fclose($fp);

    $headerLines = $metaData['wrapper_data'];

    if(!$assoc) return $headerLines;

    $headers = array();
    foreach($headerLines as $line) {
        if(strpos($line, 'HTTP') === 0) {
            $headers[0] = $line;
            continue;
        }

        list($key, $value) = explode(': ', $line);
        $headers[$key] = $value;
    }

    return $headers;
}

Called like this,

$context = stream_context_create(array('http' => array('method' => 'HEAD')));
$headers = get_headers_with_stream_context($url, $context, 1);

it gives you what you're after while leaving the standard stream_context unmodified.

Please note that this function will fail if passed anything other than an http url.

There seems to be a feature request for an additional argument for get_headers(), but the bug tracker is down as I'm writing this, so I can't check for other solutions there.

Ruffi answered 22/5, 2012 at 12:39 Comment(0)
A
5

I had a similar issue but I just used the file_get_contents function with the custom stream context instead.

Here's how I implemented it:

$options = array(
               'http' => array(
                     'method' => 'HEAD',
                     'follow_location' => 0
                )
           );

$context = stream_context_create($options);

@file_get_contents($url, NULL, $context);

var_dump($http_response_header);

Using this context only the headers will be fetched by file_get_contents and will populate the $http_response_header PHP variable.

Ancelin answered 1/7, 2014 at 3:15 Comment(0)
L
1

Instead of the accepted answer, I did the following, which will work in PHP 5.3 and above, though I haven't fully tested it. (There's also a stream_context_get_params($context) but I think this is enough.)

$stream_context_defaults = stream_context_get_options(stream_context_get_default());
stream_context_set_default(
    array(
        'http' => array(
            'method' => 'HEAD'
        )
    )
);
for ($i = 1; $i <= 10; $i++) {
    $headers = get_headers('http://www.example.org');
}
stream_context_set_default($stream_context_defaults); // reset to defaults
Liripipe answered 15/1, 2014 at 21:25 Comment(3)
This will not always work, stream_context_set_default() can not be "reset" with an empty $stream_context_defaults array.Hardcore
Is this documented somewhere? I posted this in the interests of trying to work around the lack of arguments for get_headers. I do realise that it's perhaps not the smartest to set defaults rather than create a new context, but I wanted to re-use the get_headers method. It'd be nice if get_headers had the ability to pass in a custom context, like file_get_contents.Liripipe
Haven't clearly seen it in the docs. Figured it out by testing.Hardcore
G
0

As of PHP 7.1.0, get_headers() now accepts a third parameter for context.

$context = stream_context_create(
    array(
        'http' => array('method' => 'HEAD')
    )
);

$headers = get_headers($url, true, $context);
Glass answered 11/10, 2021 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.