Search code examples
phphttp-headersfile-get-contentshttp-content-length

Ignore Content-Length header when using file_get_contents


I need to get the contents of a page, which always sends a Content-Length: 0 header, however the page is never empty.

The file_get_contents(url) just returns an empty string.

The whole header returned by the page is:

HTTP/1.1 200 OK
X-Powered-By: PHP/5.3.10
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Sat, 18 Feb 2012 18:14:59 GMT
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Date: Sat, 18 Feb 2012 18:14:59 GMT
Server: lighttpd

Would it be possible to use file_get_contents and ignore the header or do I need to use curl?

Edit

get_headers(url) output (using print_r):

Array
(
    [0] => HTTP/1.0 200 OK
    [1] => X-Powered-By: PHP/5.3.10
    [2] => Content-type: text/html
    [3] => Content-Length: 0
    [4] => Connection: close
    [5] => Date: Sat, 18 Feb 2012 22:39:52 GMT
    [6] => Server: lighttpd
)

Solution

  • As noted by Optimist the problem had nothing to do with the headers, but rather that I didn't send any User-Agent header to the server.

    file_get_contents worked perfectly after sending User-Agent headers, even though the server always returns Content-Length: 0.

    Weird.