Tom
Tom

Reputation: 23

How do I change the headers that PHP sends when using file_get_contents on an external URL?

I need to change the headers that PHP sends when it requests a file using file_get_contents(). Is that possible or would I have to use CURL?

Upvotes: 0

Views: 259

Answers (2)

Xavier Barbosa
Xavier Barbosa

Reputation: 3947

You can use file_get_contents() in conjunction with stream_context_create()

Exemple :

<?php
$context = stream_context_create(array(
    'http'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
                  "Cookie: foo=bar\r\n"
  )
));
$out = file_get_contents($filename, false, $context);

Upvotes: 5

Antti Ryts&#246;l&#228;
Antti Ryts&#246;l&#228;

Reputation: 1545

You will need to use CURL. It's also more reliable. See curl_setopt and CURLOPT_HTTPHEADER.

Upvotes: 0

Related Questions