how to know a remote webserver is finished, if it doesn't close the socket
$30-100 USD
În desfășurare
Data postării: circa 12 ani în urmă
$30-100 USD
Plata la predare
I have a simple PHP script that fetches content from remote websites. The problem is that some web servers, [login to view URL] for example, after sending the data for a page, they don't close the TCP connection, so web page download doesn't complete until the fread() call times out (after 60 seconds by default, although I can lower that with stream_set_timeout()).
On the other hand, if I use curl to fetch the same page:
curl [login to view URL] -o [login to view URL]
the operation completes almost instantly.
So here is exactly what I want to know: How does curl "know" that [login to view URL] is finished sending data (even though the server doesn't close the connection)? And how can I make my PHP script aware of that as well, so it doesn't have to wait for the connection to time out?
With this simple php script for example:
<?php
$hostname = "[login to view URL]";
$_socket = @fsockopen("tcp://$hostname", 80);
fwrite($_socket, "GET / HTTP/1.0\r\nHost: $hostname\r\nUser-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)\r\n\r\n");
do
{
$timebefore = time();
$data = @fread($_socket, 8192);
$timeafter = time();
echo "fread from $hostname: " . ($timeafter-$timebefore) . " seconds for " . strlen($data) . " bytes\n";
}
while (isset($data{0}));
?>
if I run it, I get this output:
[root@peacefire01 - /var/www/html # php [login to view URL]
fread from [login to view URL]: 0 seconds for 2901 bytes
fread from [login to view URL]: 0 seconds for 1467 bytes
fread from [login to view URL]: 0 seconds for 1520 bytes
fread from [login to view URL]: 0 seconds for 1780 bytes
fread from [login to view URL]: 60 seconds for 0 bytes
Notice the 60 seconds at the end. If, however, I replace "[login to view URL]" with "[login to view URL]" at the top, then the page downloads "normally" without a long timeout for the last fread() call.