哪个HTTP版本默认使用file_get_contents?

问题描述:

最近,我们的服务供应商之一发送此更新到他们的服务:哪个HTTP版本默认使用file_get_contents?

使用HTTP 1.0协议:
API调用将返回HTTP/1.0 400 Bad Request ...应更新其代码以HTTP 1.1,包括API请求的主机头。

PHP的file_get_contents()是否默认做这一切?

您可以通过一个stream_context像下面的代码的file_get_contents:

<?php 
// Create a stream 
$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Accept-language: en\r\n" . 
       "Cookie: foo=bar\r\n" 
) 
); 

$context = stream_context_create($opts); 

// Open the file using the HTTP headers set above 
$file = file_get_contents('http://www.example.com/', false, $context); 
?> 

您可以找到更多的PHP手册: http://us.php.net/file_get_contents

希望它能帮助。

+0

这将如何改变使用的协议_version_? –

+0

检查上述有关更改协议的答案。 基本上,您可以根据需要更改请求的标题。您也可以添加或删除任何标题。 :) – DenisR

根据http://de1.php.net/manual/de/context.http.php默认的上下文protocol_version1.0。你应该只需通过

<?php 
$opts = array(
    'http'=>array(
    'method'=>"GET" 
) 
); 

$context = stream_context_create($opts, 
        array ('protocol_version'=> '1.1') 
); 

$file = file_get_contents('http://www.example.com/', false, $context); 
?> 

设置(见http://de1.php.net/stream_context_create

使用在context参数file_get_contentshttp元素protocol_version选项。它默认为版本1.0。有关更多详细信息,请参阅HTTP Context Options