PHP 文件获取内容,http 版本

Tob*_*run 3 php http file-get-contents

我正在使用 file_get_contents 函数来控制客户端,(例如http://ip:port/?light=on

在浏览器中使用相应的命令时它可以工作,当我将相同的 url 与 file_get_contents 函数结合使用时,它不起作用。

当我使用wireshark 请求时,我注意到浏览器使用的是http/1.1,而file_get_contents 使用的是http/1.0。

我相信 http 的版本是我的代码不起作用的问题,

如何在 de file_get_contents 函数中更改此版本的 http?或解决它?

alg*_*net 5

您可以使用上下文设置 HTTP 版本:

$context = stream_context_create(array('http'=>array('protocol_version'=>'1.1')));
file_get_contents('http://ip:port/?light=on', false, $context);
Run Code Online (Sandbox Code Playgroud)

另请参阅上下文选项的完整列表http://www.php.net/manual/en/context.http.php

请注意,如果服务器使用分块编码,则必须使用 PHP 5.3 或更高版本。