开发者

php fsockopen fgets become too slow

I got a problem when I was trying to use fsockopen and fgets to get the content of a web page. I can easily receive the header from the web server as follow:

HTTP/1.1 200 OK 
[cache-control] => private 
[content-type] => text/plain; charset=utf-8 
[server] => Microsoft-IIS/7.0
[x-aspnet-version] => 2.0.50727
[x-powered-by] => ASP.NET
[date] => Sat, 23 Jul 2011 10:36:57 GMT
[connection] => close
[c开发者_JAVA百科ontent-length]=> 30072 
[vary] => Accept-Encoding [content-encoding] => gzip

My code is as follow:

$fp = @fsockopen(“www.abc.com”, 80, $errno, $errstr, 30);
fwrite($fp, $request);//$request is my predefined header including path and cookies
$lines=””;
while (!feof($fp)) 
{
  Echo “fgets start at ”.date(“H:i:s”).”\n”;
  $line = fgets($fp, 4096);
  Echo strlen($line) .”\n”;
  Echo “fgets end at ”.date(“H:i:s”) .”\n”;
  $lines +=$line;
}
fclose($fp);

the output was like that:

……
fgets start at 12:23:45
219
Fgets end at 12:23:47
……

I am really wondering why it takes 2 seconds to get just 219 byte data.it is just too slow. To get the whole page,including hundreds of fgets iteration, it spends 40 seconds. Whereas if you use firefox, it just a second..

I want to know what causes the fgets become too slow.

Thanks for your reading.


out of the top my head: is it possible that the connection is kept open because you're using a HTTP/1.1 request? this would make the server wait for you to make another request, then close the connection if none found (taken as a timeout). besides that, your operating system might be buffering the data (SO_RCVLOWAT socket_option) so the read ends when the web server closes the connection.

try reducing the amount of data to be read (like 128 bytes) inside a loop, and see what happens. other than that (and related to this), you might want to try reading this: http://ar.php.net/manual/en/filesystem.configuration.php#ini.auto-detect-line-endings

php might not be reading well the EOL's

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜