【发布时间】:2015-05-25 22:00:05
【问题描述】:
为什么fsockopen 与来自浏览器的相同请求相比如此缓慢?
php fsockopen:0.254秒
浏览器:0.070 秒
fsockopen 请求
$time = microtime(true);
if($fp = fsockopen('ssl://domain.com', 443, $errno, $errstr, 20)){
echo "\n".(microtime(true) - $time);
$this->request = 'POST '.$path.' HTTP/1.1'.$crlf
.'Host: '.$this->host.$crlf
.'Content-Type: application/x-www-form-urlencoded'.$crlf
.'Content-Length: '.$content_length.$crlf
.'Connection: Close'.$crlf.$crlf
.$body;
fwrite($fp, $this->request);
while($line = fgets($fp)){
if($line !== false){
$this->response .= $line;
}
}
fclose($fp);
}
echo "\n".(microtime(true) - $time);
fsockopen 结果
0.18865990638733
0.25424790382385
来自浏览器的请求
【问题讨论】:
-
dns 缓存?反向dns查找时间?网络呼叫不仅仅是在线上的飞行时间。
-
如何在 fsockopen 中使用 DNS 缓存?
-
你没有。这是在系统解析器级别完成的。
-
你的意思是
/etc/hosts或c:\windows\system32\drivers\etc\hosts? -
节点。这只是硬编码 dns 映射,与解析器无关。