fsockopen 似乎很慢
fsockopen seems very slow
为什么这个 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
来自浏览器的请求
可能是EOF问题,你的fopen等待超时
尝试缩短超时时间以获得更快的速度 return,但这不是一个优雅的解决方案。
其他解决方案是使用 bucle 手动查询连接,如下例:
while (!feof($conn)) {
print fgets($conn, 1024);
}
示例来源:
我在这里找到了解决方案
if connection is keep alive how to read until end of stream php
为什么这个 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
来自浏览器的请求
可能是EOF问题,你的fopen等待超时
尝试缩短超时时间以获得更快的速度 return,但这不是一个优雅的解决方案。
其他解决方案是使用 bucle 手动查询连接,如下例:
while (!feof($conn)) {
print fgets($conn, 1024);
}
示例来源:
我在这里找到了解决方案
if connection is keep alive how to read until end of stream php