note 45584 added to function.fsockopen
| From: | MichaelBrown at osu1 dot php dot net | Date: | Sat, 11 Sep 2004 23:54:14 +0000 |
| Subject: | note 45584 added to function.fsockopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76488@lists.php.net to get a copy of this message | ||
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!
----
Manual Page -- http://www.php.net/manual/en/function.fsockopen.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45584
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45584&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45584&report=yes
Search -- http://master.php.net/manage/user-notes.php