note 101026 modified in function.file-get-contents by danbrown
| From: | danbrown@php.net | Date: | Mon, 22 Nov 2010 13:58:26 +0000 |
| Subject: | note 101026 modified in function.file-get-contents by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174255@lists.php.net to get a copy of this message | ||
If your file_get_contents freezes during several seconds, here is maybe your answer:
Beware that the default keepalive timeout of Apache 2.0 httpd is 15 seconds. This is true for
HTTP/1.1 connections, which is not the default behavior of file_get_contents but you can force it,
especially if you are trying to act as a web browser. I don't know if this is also the case for
HTTP/1.0 connections.
Forcing the server to close the connection would make you gain those 15 seconds in your script:
<?php
$context = stream_context_create(array('http' =>
array('header'=>'Connection: close')));
$content = file_get_contents("http://www.example.com/test.html");
?>
Another way of resolving slowness issues is to use cURL or fsockopen. Bear in mind that contrary to
the behavior of web browsers, file_get_contents doesn't return the result when the web page is
fully downloaded (i.e. HTTP payload length = value of the response HTTP "Content-Length"
header) but when the TCP connection is closed.
I hope this behavior will change in future releases of PHP.
This has been experienced with PHP 5.3.3.
--was--
If your file_get_contents freezes during several seconds, here is maybe your answer:
Beware that the default keepalive timeout of Apache 2.0 httpd is 15 seconds. This is true for
HTTP/1.1 connections, which is not the default behavior of file_get_contents but you can force it,
especially if you are trying to act as a web browser. I don't know if this is also the case for
HTTP/1.0 connections.
Forcing the server to close the connection would make you gain those 15 seconds in your script:
$context = stream_context_create(array('http' =>
array('header'=>'Connection: close')));
$content = file_get_contents("http://www.somesite.com/test.html");
Another way of resolving slowness issues is to use cURL or fsockopen. Bear in mind that contrary to
the behavior of web browsers, file_get_contents doesn't return the result when the web page is
fully downloaded (i.e. HTTP payload length = value of the response HTTP "Content-Length"
header) but when the TCP connection is closed.
I hope this behavior will change in future releases of PHP.
This has been experienced with PHP 5.3.3.
http://php.net/manual/en/function.file-get-contents.php