php – 使用fgets / fread挂起从fsockopen读取数据
发布时间:2020-12-13 13:16:48 所属栏目:PHP教程 来源:网络整理
导读:这是我正在使用的代码: 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"."rn");while (!feof($fp))
这是我正在使用的代码:
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"."rn"); 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并且循环将永远继续.
当你编写一个完整的实现时,这不会有问题,因为你将寻找响应代码并相应地突破循环,例如: <?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 CAPABILITYrn"); echo " Donern"; // 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!"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |