note 101026 deleted from function.file-get-contents by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:38:25 +0000 |
| Subject: | note 101026 deleted from function.file-get-contents by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215241@lists.php.net to get a copy of this message | ||
Note Submitter: KrisWebDev
----
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.