【发布时间】:2011-10-11 14:50:22
【问题描述】:
我在尝试使用 fsockopen 和 fgets 获取网页内容时遇到问题。我可以很容易地从 Web 服务器接收标头,如下所示:
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
[content-length]=> 30072
[vary] => Accept-Encoding [content-encoding] => gzip
我的代码如下:
$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);
输出是这样的:
……
fgets start at 12:23:45
219
Fgets end at 12:23:47
……
我真的很想知道为什么只需要 2 秒才能获得 219 字节的数据。这太慢了。 要获取整个页面,包括数百个 fget 迭代,它需要花费 40 秒。而如果您使用 Firefox,则只需一秒钟..
我想知道是什么原因导致 fgets 变得太慢。
感谢您的阅读。
【问题讨论】:
-
嗯,
file_get_contents("www.abc.com")需要多少时间? -
你找到解决这个@cn2500的方法了吗?
标签: php performance header fgets fsockopen