GrimReaper
GrimReaper

Reputation: 13

Can you add http headers to be sent on a crossdomain fopen call?

I mean, I want to open an image file which is on another website, but they have some sort of server side script which only provides the image in case a certain cookie is set on the browser.

Is it possible for me to make a fopen call sending in a cookie header?

Upvotes: 1

Views: 6159

Answers (1)

Andre D
Andre D

Reputation: 4713

Yes, you can control the HTTP request, including adding cookies, by configuring the HTTP stream context.

Check out this example from the manual: http://php.net/stream-context-create

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

/* Sends an http request to www.example.com
   with additional headers shown above */
$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);

See also http://php.net/manual/en/context.http.php for all HTTP context options.

Upvotes: 7

Related Questions