Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file_get_contents() and setting request headers

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?

like image 388
Marcus Avatar asked Jan 21 '10 08:01

Marcus


People also ask

What is the use of file_get_contents () function?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

What will the file_get_contents () return?

The file_get_contents() function returns Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. An E_WARNING level error is generated if filename cannot be found, maxlength is less than zero, or if seeking the specified offset in the stream fails.

What is the difference between file_get_contents ($ file and file_get_contents ($ file in PHP?

2 Answers. Show activity on this post. The first two, file and file_get_contents are very similar. They both read an entire file, but file reads the file into an array, while file_get_contents reads it into a string.

Is file_get_contents slow?

it takes anywhere from 30-90 seconds to process. It's not limited to our server, it is slow when accessing any external url, such as http://www.google.com. I believe the script calls the full url because there are query string variables that are necessary that don't work if you call the file locally.


2 Answers

Actually, upon further reading on the file_get_contents() function:

// Create a stream $opts = [     "http" => [         "method" => "GET",         "header" => "Accept-language: en\r\n" .             "Cookie: foo=bar\r\n"     ] ];  // DOCS: https://www.php.net/manual/en/function.stream-context-create.php $context = stream_context_create($opts);  // Open the file using the HTTP headers set above // DOCS: https://www.php.net/manual/en/function.file-get-contents.php $file = file_get_contents('http://www.example.com/', false, $context); 

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)

like image 93
Dominic Barnes Avatar answered Oct 29 '22 00:10

Dominic Barnes


Here is what worked for me (Dominic was just one line short).

$url = "";  $options = array(   'http'=>array(     'method'=>"GET",     'header'=>"Accept-language: en\r\n" .               "Cookie: foo=bar\r\n" .  // check function.stream-context-create on php.net               "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad    ) );  $context = stream_context_create($options); $file = file_get_contents($url, false, $context); 
like image 29
Fabrice Avatar answered Oct 28 '22 23:10

Fabrice