fsockopen似乎很慢

时间:2015-05-25 22:00:05

标签: php fsockopen

与来自浏览器的相同请求相比,为什么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
来自浏览器的

请求

enter image description here

2 个答案:

答案 0 :(得分:0)

可能是EOF问题,你的fopen等到超时。

尝试较低的超时以获得更快的回报,但这不是一个优雅的解决方案。

其他解决方案是使用bucle手动查询连接,如下例所示:

while (!feof($conn)) {
    print fgets($conn, 1024);
}

示例来源: https://stackoverflow.com/a/1319434/3518053

答案 1 :(得分:-1)

相关问题