3

I'm using the file_get_contents function to control a client, (e.g. http://ip:port/?light=on)

When using the corresponding command in the browser it works, when i use the same url in combination with file_get_contents function it doesnt work.

When i wireshark the requests i notice that the browser is using http/1.1 and file_get_contents is using http/1.0.

I believe that the version of http is the problem why my code is not working,

How can i change this version of http in de file_get_contents function? or work around it?

2
  • 1
    I would use a curl solution instead - stackoverflow.com/questions/3979802/… Commented Apr 23, 2012 at 12:05
  • Are you sure allow_url_fopen is set accordingly? , also consider using curl Commented Apr 23, 2012 at 12:05

1 Answer 1

5

You can set the HTTP version using a context:

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

See also the full list of context options http://www.php.net/manual/en/context.http.php

Note that if the server uses chunked encoding, you must use PHP 5.3 or superior.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.