I'm using PHP code to retrieve a resource from an HTTP server via PHP HTTP wrapper, like:
file_get_contents("http://...");
While the PHP sends HTTP/1.0 request, the server responds with HTTP/1.1 response with Connection: Keep-Alive header:
HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Length: ...
Although the PHP has no way to use the persistent connection, it seems to pointlessly wait for the HTTP connection to close. The server closes it only after 60 seconds of inactivity.
Is there any way to prevent PHP HTTP wrapper from waiting for the server to close the connection, but close it itself after receiving the data announced by Content-Length header?
Or is it at least safe to use the fopen and the fread loop, reading until I get the Content-Length bytes?
The only workaround I've found is to set the timeout HTTP context option to reduce the wait time. But obviously I need to set the timeout high enough to be sure it does not interrupt reading of the response, so it's still far from ideal.
Other approaches I've tried without success:
- use the HTTP/1.1 (using the
protocol_versioncontext option), hoping that it will make PHP be aware of persisted connection - use the
Connection: closeheader (the server ignores it)
fopen+freadwith an explicit use of theContent-Lengthheader. - Martin Prikryl