2

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?

j08691
  • 204,283
  • 31
  • 260
  • 272
superhero
  • 6,281
  • 11
  • 59
  • 91
  • ofc, how would that give me the answer? I would have to metier band with or something to get the answer some other way. Or did you not understand the question? – superhero Oct 06 '13 at 23:02
  • curl is faster, if that matters to you –  Oct 06 '13 at 23:02
  • @ErikLandvall try with a file on your own server, then look in the access log, you will see how much actual data was transferred. –  Oct 06 '13 at 23:04
  • You can also send a HEAD request with cURL using the `CURLOPT_CUSTOMREQUEST` option. – George Brighton Oct 06 '13 at 23:04
  • I know about curl, have a look at this question wich is why I'm doing it this way: http://stackoverflow.com/questions/19212977/curl-retrive-headers-empty-response/19214139?noredirect=1#19214139 – superhero Oct 06 '13 at 23:04
  • so the real question is getting curl to work –  Oct 06 '13 at 23:11
  • @Dragon, Not addressed in this question though, but If you would like to address it in the question I linked to I would be happy ofc :) – superhero Oct 06 '13 at 23:12

3 Answers3

1

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
Zwirbelbart
  • 777
  • 2
  • 8
  • 19
0

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.

Standard
  • 1,450
  • 17
  • 35
0

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');
?>
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
  • yes I know this, I just don't feel that it's clear if it really retrieves the whole file or not even though it uses the GET request. It makes no sense. its just a request that the server could handle any way it wonts to. It should be up to the function to stop ones it hits the end of the headers I would say .. Also, I could specify different result depending on the request wich means that I may not get the same information if I change from GET to HEAD, I have also noticed that this is that case some of the times.. – superhero Oct 06 '13 at 23:10
  • I can't really answer that. The only information presented from the docs is that it by default uses GET, and most likely just strips away the body-part. There's no way to do a GET-request and just "quit" once the HEAD-part is buffered. I don't really see any way to explain the different results either. It should not do that. – OptimusCrime Oct 06 '13 at 23:15
  • Why wouldn't it be able to stop? Like I stated, the request method is just a request. I need to force the knowledge of the mimetype thrown by the server for the file so I know if the request body is of any interest.. – superhero Oct 06 '13 at 23:22
  • I don't really get the point. Why don't you request the header and if it's "good", request the body? – Standard Oct 06 '13 at 23:34
  • I agree with @wernersbacher. If this code is dealing with huge files, doing multiple requests are the...only way (as I see it). If the code is loading tiny files it would not matter much anyways, and a normal GET would do. – OptimusCrime Oct 06 '13 at 23:36
  • This is what I wont, but a request is as stated before only a request. I need to know if I can force it. I don't believe that just by asking for something will grant me this future even if it may work most of the time. But also, the method used to request something is part of the url and can make the server respond differently by specifying different methods. Have a look at this question: http://stackoverflow.com/q/12210816/570796 – superhero Oct 06 '13 at 23:48