add headers to file_get_contents in php

后端 未结 2 1430

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

2条回答
  •  青春惊慌失措
    2021-02-10 04:13

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

提交回复
热议问题