2012-04-23 45 views
1

我使用的file_get_contents函数来控制客户端, (如http://ip:port/?light=onPHP文件中获取内容,HTTP版本

当使用它的工作原理浏览器相应的命令,当我使用相同的URL组合与file_get_contents功能它不工作。

当我wireshark请求我注意到浏览器正在使用http/1.1和file_get_contents使用http/1.0。

我认为,HTTP的版本就是为什么我的代码不工作的问题,

我怎么能在德的file_get_contents功能改变这个HTTP版本?或解决它?

+1

我会使用卷曲解决方案,而不是 - http://stackoverflow.com/questions/3979802/alternative-to-file-get-contents – 2012-04-23 12:05:17

+0

你确定allow_url_fopen是相应地设置? ,也考虑使用卷曲 – 2012-04-23 12:05:32

回答

3

可以利用上下文来设置HTTP版本:

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

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

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