Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add headers to file_get_contents in php

Tags:

php

I am completely new PHP and want a client program to call an URL web service.I am using file_get_content to get the data.How do add additional headers to the request made using file_get_content.

I also was thinking of using cURL. I wanted to know how cURL can be used to do a GET request.

like image 672
frictionlesspulley Avatar asked Jan 24 '11 07:01

frictionlesspulley


1 Answers

You can add headers to file_get_contents, it takes a parameter called context that can be used for that:

$context = stream_context_create(array(
    'http' => array(
        'method' => 'GET',
        'header' => "Host: www.example.com\r\n" .
                    "Cookie: foo=bar\r\n"
    )
));
$data = file_get_contents("http://www.example.com/", false, $context);
like image 184
reko_t Avatar answered Oct 04 '22 20:10

reko_t