使用 SSL 时 PHP fread 挂起

发布于 2024-12-06 01:39:09 字数 500 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳公公是暖光 2024-12-13 01:39:09

我相信 fread 挂起,因为在最后一个块上,它期望 2048 字节的信息,并且可能得到的信息少于该信息,因此它会等待直到超时。

您可以尝试像这样重构您的代码:

$bytes_to_read = 2048;
while ($chunk = fread($connection, $bytes_to_read)) {
  $response .= $chunk;
  $status = socket_get_status ($connection);
  $bytes_to_read = $status["unread_bytes"];
}

这样,您将分两块读取所有内容...我还没有测试过这段代码,但我记得不久前遇到过类似的问题,并用类似的方法修复它。

希望有帮助!

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:

$bytes_to_read = 2048;
while ($chunk = fread($connection, $bytes_to_read)) {
  $response .= $chunk;
  $status = socket_get_status ($connection);
  $bytes_to_read = $status["unread_bytes"];
}

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文