PHP fsockopen很慢 [英] PHP fsockopen Is Slow

查看:182
本文介绍了PHP fsockopen很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用fsockopen来发送和接收命令,在PHP中使用IMAP协议.我的初步实验可以正常进行,但是速度很慢.下面的简单功能运行大约需要2分钟.我尝试了几种不同的IMAP服务器,并获得了相同的结果.谁能告诉我为什么这段代码这么慢?

I'm playing around with the IMAP protocol in PHP using fsockopen to send and receive commands. My preliminary experiments work but are insanely slow. It takes about 2 minutes for the simple function below to run. I've tried several different IMAP servers and have gotten the same result. Can anyone tell me why this code is so slow?

<?php

function connectToServer($host, $port, $timeout) {
    // Connect to the server
    $conn = fsockopen($host, $port, $errno, $errstr, $timeout);

    // Write IMAP Command
    $command = "a001 CAPABILITY\r\n";

    // Send Command
    fputs($conn, $command, strlen($command));

    // Read in responses
    while (!feof($conn)) {
        $data .= fgets($conn, 1024);
    }

    // Display Responses
    print $data;

    // Close connection to server
    fclose($conn);
}

connectToServer('mail.me.com', 143, 30);

?>

这是我得到的回复:

macinjosh:Desktop Josh$ php test.php
* OK [CAPABILITY mmp0613 IMAP4 IMAP4rev1 ACL QUOTA LITERAL+ NAMESPACE UIDPLUS CHILDREN BINARY UNSELECT SORT LANGUAGE IDLE XSENDER X-NETSCAPE XSERVERINFO X-SUN-SORT X-SUN-IMAP X-ANNOTATEMORE X-UNAUTHENTICATE XUM1 AUTH=PLAIN STARTTLS] Messaging Multiplexor (Sun Java(tm) System Messaging Server 6.3-6.03 (built Jun  5 2008))
* CAPABILITY mmp0613 IMAP4 IMAP4rev1 ACL QUOTA LITERAL+ NAMESPACE UIDPLUS CHILDREN BINARY UNSELECT SORT LANGUAGE IDLE XSENDER X-NETSCAPE XSERVERINFO X-SUN-SORT X-SUN-IMAP X-ANNOTATEMORE X-UNAUTHENTICATE XUM1 AUTH=PLAIN STARTTLS
a001 OK CAPABILITY completed

推荐答案

在远程端超时并关闭连接之前,feof似乎不会返回true.您传递的$timeout参数仅适用于初始连接尝试.

It seems like feof won't return true until the remote side times out and closes the connection. The $timeout parameter that you are passing only applies to the initial connection attempt.

尝试更改while循环以直接打印状态:

Try changing your while loop to print the status directly:

while (!feof($conn)) {
    print fgets($conn, 1024);
}

或者将其循环退出条件更改为在读取完整答复后中断.该协议可能必须更加聪明.

Or change your loop exit condition to break after its read the full reply. It would probably have to be smarter about the protocol.

最后,我不得不问,为什么不使用PHP的内置IMAP客户端 ?

Finally, I have to ask, why aren't you using PHP's built-in IMAP client?

这篇关于PHP fsockopen很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆