superhero
superhero

Reputation: 6482

How does get_headers work in the background

I tryied searching for this and I belive I alredy know the answer but it's crusal that I'm not wrong, so here I go..

When calling get_headers, will I retrieve the whole file even though the function only returns the headers or will it retrieve, as expected, only the headers and nothing else?

I'm guessing the last but if I'm wrong this will cause some serious problems.. Also I noticed that there is a global setting I can change to send a HEAD request instead of the default GET request, witch is why I'm asking my self whats really going on.


Edit

Maybe this function is a better alternative? stream_get_meta_data or do they actually do the same thing?

Upvotes: 2

Views: 789

Answers (3)

Zwirbelbart
Zwirbelbart

Reputation: 827

You could also take a look at the source code, if you are familiar with C.

The function is defined here. I quickly looked over this, and it seems it is a header-only request, see line 715:

STREAM_ONLY_GET_HEADERS

Upvotes: 1

Standard
Standard

Reputation: 1512

Unfortunaley you're right, just read the PHP manual:

get_headers() returns an array with the headers sent by the server in response to a HTTP request.

Also take a look at the examples.

Okay, next time I should spend more attention to the question formulation.

Yeh, if the request type is set to GET (standard) you will get the whole content. You could change it to HEAD, but this is not what you want.

Upvotes: 0

OptimusCrime
OptimusCrime

Reputation: 14863

GET

Requests a representation of the specified resource. Requests using GET should only retrieve data and should have no other effect. (This is also true of some other HTTP methods.) The W3C has published guidance principles on this distinction, saying, "Web application design should be informed by the above principles, but also by the relevant limitations."

HEAD

Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

Wikipedia/Hypertext_Transfer_Protocol

The PHP-docs clearly states that normal get_headers() uses a GET-request, but you can force it to use HEAD instead, like this:

<?php
// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
    array(
        'http' => array(
            'method' => 'HEAD'
        )
    )
);
$headers = get_headers('http://example.com');
?>

Upvotes: 0

Related Questions