With PHP, is it possible to send HTTP headers with file_get_contents()
?
I know you can send the user agent from your php.ini
file. However, can you also send other information such as HTTP_ACCEPT
, HTTP_ACCEPT_LANGUAGE
, and HTTP_CONNECTION
with file_get_contents()
?
Or is there another function that will accomplish this?
Actually, upon further reading on the
file_get_contents()
function:You may be able to follow this pattern to achieve what you are seeking to, I haven't personally tested this though. (and if it doesn't work, feel free to check out my other answer)
Unfortunately, it doesn't look like
file_get_contents()
really offers that degree of control. The cURL extension is usually the first to come up, but I would highly recommend the PECL_HTTP extension (http://pecl.php.net/package/pecl_http) for very simple and straightforward HTTP requests. (it's much easier to work with than cURL)If you don't need HTTPS and curl is not available on your system you could use
fsockopen
This function opens a connection from which you can both read and write like you would do with a normal file handle.
Using the php cURL libraries will probably be the right way to go, as this library has more features than the simple
file_get_contents(...)
.An example:
You can use this variable to retrieve response headers after
file_get_contents()
function.Code:
Output:
Here is what worked for me (Dominic was just one line short).