php 使用 fgets/fread 从 fsockopen 读取数据挂起
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10449540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading data from fsockopen using fgets/fread hangs
提问by victor_golf
Here is the code that I am using:
这是我正在使用的代码:
if (!($fp = fsockopen('ssl://imap.gmail.com', '993', $errno, $errstr, 15)))
echo "Could not connect to host";
$server_response = fread($fp, 256);
echo $server_response;
fwrite($fp, "C01 CAPABILITY"."\r\n");
while (!feof($fp)) {
echo fgets($fp, 256);
}
I get the first response:
我得到的第一反应是:
OK Gimap ready for requests from xx.xx.xx.xx v3if9968808ibd.15
but then the page times out. I have searched through stream_set_blocking, stream_set_timeout, stream_select, fread, etc. but could not get it to work. I need to read all the data that the server sends and then proceed with other commands (I would be retrieving emails using imap).
但随后页面超时。我已经搜索了 stream_set_blocking、stream_set_timeout、stream_select、fread 等,但无法让它工作。我需要读取服务器发送的所有数据,然后继续执行其他命令(我将使用 imap 检索电子邮件)。
Thanks
谢谢
采纳答案by DaveRandom
Your script is hanging in the while loop at the end. This is because you have used !feof()as the condition for the loop, and the server is not closing the connection. This means the feof()will always return falseand the loop will continue forever.
您的脚本最后挂在 while 循环中。这是因为您已用作!feof()循环的条件,并且服务器未关闭连接。这意味着feof()将始终返回false并且循环将永远继续。
This will not be problem when your write a full implementation, as you will be looking for response codes and can break out of the loop accordingly, for example:
当您编写完整的实现时,这不会成为问题,因为您将寻找响应代码并可以相应地跳出循环,例如:
<?php
// Open a socket
if (!($fp = fsockopen('ssl://imap.gmail.com', 993, $errno, $errstr, 15))) {
die("Could not connect to host");
}
// Set timout to 1 second
if (!stream_set_timeout($fp, 1)) die("Could not set timeout");
// Fetch first line of response and echo it
echo fgets($fp);
// Send data to server
echo "Writing data...";
fwrite($fp, "C01 CAPABILITY\r\n");
echo " Done\r\n";
// Keep fetching lines until response code is correct
while ($line = fgets($fp)) {
echo $line;
$line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
$code = $line[0];
if (strtoupper($code) == 'C01') {
break;
}
}
echo "I've finished!";
回答by Woutifier
Your script should be working. In fact, it is working.
您的脚本应该可以正常工作。事实上,它正在发挥作用。
See the results below on my pc when I ran your code:
当我运行你的代码时,在我的电脑上看到下面的结果:
* OK Gimap ready for requests from xx.xx.xx.xx l5if4585958ebb.20
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH
C01 OK Thats all she wrote! l5if4585958ebb.20
Since gmail doesn't disconnect you. No end of file occurs. And the page loading simply times out.
由于 gmail 不会断开您的连接。不会出现文件结尾。并且页面加载只是超时。
In other words: Your script will just keep waiting and waiting until gmail does disconnect, which unfortunately happens after your page load has already timed out.
换句话说:您的脚本将一直等待直到 gmail 断开连接,不幸的是,这发生在您的页面加载已经超时之后。

