在外部URL上使用file_get_contents时,如何更改PHP发送的标头?

Tom*_*Tom 0 php header file-get-contents

我需要更改PHP在使用file_get_contents()请求文件时发送的标头.这是可能的还是我必须使用CURL?

Xav*_*osa 5

您可以将file_get_contents()stream_context_create()结合使用

例如:

<?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);
Run Code Online (Sandbox Code Playgroud)