|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-01-10 16:56 UTC] jim at bluedojo dot com
Description: ------------ fgets() hangs infintely on some urls. This url: http://www.nwf.org/productions/whales.html will not do anything and should return false when I use stream_set_timeout() but it doesn't time out. I tried fgets($fd, 1024) and fgets($fd, 4096) but that doesn't work either. Reproduce code: --------------- $url = http://www.nwf.org/productions/whales.html; if ($fd = @fopen($url,'rb')){ stream_set_timeout($fd, 6); $html = ''; while (!feof($fd)) { $html .= trim(fgets($fd)); } fclose($fd); } Expected result: ---------------- The code should store the html code of "$url" into "$html." But it hangs on some urls when it should return false due to stream_set_timeout(); Actual result: -------------- fgets() Hangs infinitely. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 16:00:02 2025 UTC |
I suspect the problem to be with feof() rather than fgets(). try this: do { $line = fgets($fp); if ($line === false) break; $html .= trim($line); } while(true);I will try to answer the question the best I can. I have written a spider in php that can index millions of pages. Every once in a while it will encounter a page that will not load up (which I thought was due to fgets). When I type this url in the location bar of a browser, the page seems like it will load forever and nothing will show up. When I set stream_set_timeout($fd, 6) then once would expect that $fd will time out in 6 seconds and exit the loop. I believe that feof would detect that the stream would time out: if ($fd = @fopen($url,'rb')){ stream_set_timeout($fd, 6); $html = ''; while (!feof($fd)) { $html .= trim(fgets($fd)); } fclose($fd); } To answer wez's question, I had to find a url that didn't work (that took forever to load) in order to test that feof would exit due to the timing out of the stream ($fd). The url that I was using wasn't working for about one day. Then it started to load normally in a browser a day later so I couldn't use it anymore as a test case for this problem. Making the stream time out is important for my application because it needs to move on to the next url or else it will loop forever. Hope that helps.