开发者

add headers to file_get_contents in 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 cU开发者_如何学GoRL can be used to do a GET request.


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);


As for cURL, the basic example from the PHP manual shows you how to perform a GET request:

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜