note 45584 deleted from function.fsockopen by aidan
| From: | aidan@php.net | Date: | Sun, 12 Sep 2004 02:20:10 +0000 |
| Subject: | note 45584 deleted from function.fsockopen by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76499@lists.php.net to get a copy of this message | ||
Note Submitter: Michael Brown
----
The OFFICIAL fix to feof() hanging, and getting all the data back for large HTTP requests (or
small):
$fd=@fsockopen($url,80,$errno,$errstr,10); // specify 10 sec connection timeout
if($fd){
stream_set_timeout($fd,10); // set I/O timeout
fwrite($fd,$yourhttprequest);
$contents="";
while(!feof($fd) && !$stat["timed_out"]){
$contents.=fgets($fd,1024);
$stat=socket_get_status($fd);
}
if($stat["timed_out"]){
die("I/O timed out. (10 seconds)");
}
fclose($fd);
echo $contents;
}else{
echo "Connection failed.";
}
This code has been tested against the feof problem, and against large data coming back in a HTTP
request. You get all the data, and it will wait up to 10 seconds between each successful read
request, so it wont timeout if your request just takes a long time to download big files (unless it
doesnt receive data for 10 consecutive seconds). Enjoy!