这是我正在使用的代码:
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); }
我收到了第一个回复:
OK Gimap ready for requests from xx.xx.xx.xx v3if9968808ibd.15
但随后页面超时.我搜索了stream_set_blocking,stream_set_timeout,stream_select,fread等,但无法让它工作.我需要读取服务器发送的所有数据,然后继续执行其他命令(我将使用imap检索电子邮件).
谢谢
您的脚本最后挂在while循环中.这是因为您使用了!feof()作为循环的条件,并且服务器没有关闭连接.这意味着feof()将始终返回false并且循环将永远继续.
原文链接:https://www.f2er.com/php/136785.html当你编写一个完整的实现时,这不会有问题,因为你将寻找响应代码并相应地突破循环,例如:
<?PHP // Open a socket if (!($fp = fsockopen('ssl://imap.gmail.com',993,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,PREG_SPLIT_NO_EMPTY); $code = $line[0]; if (strtoupper($code) == 'C01') { break; } } echo "I've finished!";