使用 SSL 时 PHP fread 挂起
我正在使用 fsockopen
连接到 OpenVAS 管理器并发送 XML。我使用的代码是:
$connection = fsockopen('ssl://'.$server_data['host'], $server_data['port']);
stream_set_timeout($connection, 5);
fwrite($connection, $xml);
while ($chunk = fread($connection, 2048)) {
$response .= $chunk;
}
但是,在读取前两块数据后,PHP 挂在 fread 上,并且在 5 秒后不会超时。我尝试过使用stream_get_contents,它给出了相同的结果,但是如果我只使用一个fread,它就可以正常工作,只是我想读取所有内容,无论长度如何。
我猜测,这是 OpenVAS 的问题,它没有按照 PHP 期望的方式结束流,但这是一个盲目的尝试。我如何读取流?
I'm using fsockopen
to connect to an OpenVAS manager and send XML. The code I am using is:
$connection = fsockopen('ssl://'.$server_data['host'], $server_data['port']);
stream_set_timeout($connection, 5);
fwrite($connection, $xml);
while ($chunk = fread($connection, 2048)) {
$response .= $chunk;
}
However after reading the first two chunks of data, PHP hangs on fread and doesn't time out after 5 seconds. I have tried using stream_get_contents
, which gives the same result, BUT if I only use one fread, it works ok, just that I want to read everything, regardless of length.
I am guessing, it is an issue with OpenVAS, which doesn't end the stream the way PHP expects it to, but that's a shot in the dark. How do I read the stream?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信 fread 挂起,因为在最后一个块上,它期望 2048 字节的信息,并且可能得到的信息少于该信息,因此它会等待直到超时。
您可以尝试像这样重构您的代码:
这样,您将分两块读取所有内容...我还没有测试过这段代码,但我记得不久前遇到过类似的问题,并用类似的方法修复它。
希望有帮助!
I believe that fread is hanging up because on that last chunk, it is expecting 2048 bytes of information and is probably getting less that that, so it waits until it times out.
You could try to refactor your code like this:
That way, you'll read everything in two chunks.... I haven't tested this code, but I remember having a similar issue a while ago and fixing it with something like this.
Hope it helps!