Sending HTTP Headers with PHP's file_get_contents()
Although the file_get_contents() function in PHP does not offer direct support for sending HTTP headers, there are alternative approaches to achieve this.
One method is to create a stream context and configure the HTTP headers within it. This involves setting up an array called $opts that specifies the HTTP method (GET in this example), and adding the desired headers in the header key. For example:
// Create a stream $opts = [ "http" => [ "method" => "GET", "header" => "Accept-language: en\r\n" . "Cookie: foo=bar\r\n" ] ]; // Create the stream context $context = stream_context_create($opts);
Once the stream context is created, you can pass it as the third argument to file_get_contents() to send the HTTP headers along with the request:
// Send the request with headers $file = file_get_contents('http://www.example.com/', false, $context);
The above is the detailed content of How Can I Send HTTP Headers with PHP's file_get_contents()?. For more information, please follow other related articles on the PHP Chinese website!