【问题标题】:Zend Imap Connection timeoutZend Imap 连接超时
【发布时间】:2010-10-14 20:42:32
【问题描述】:

我正在使用 Zend 框架提供的 IMAP 类通过 imap 访问 gmail 消息。我一一访问收件箱中所有邮件的邮件头,并在本地对其进行索引。该脚本适用于邮件少于 10000 条的收件箱。对于较大的收件箱,该脚本会断开连接,可能是超时。

这是堆栈跟踪:

异常消息:无法读取 - 连接已关闭?

追踪:

#0 /home/dev/trunk/Zend/Mail/Protocol/Imap.php(168): Zend_Mail_Protocol_Imap->_nextLine()
#1 /home/dev/trunk/Zend/Mail/Protocol/Imap.php(285): Zend_Mail_Protocol_Imap->_nextTaggedLine(NULL)
#2 /home/dev/trunk/Zend/Mail/Protocol/Imap.php(587): Zend_Mail_Protocol_Imap->readLine(NULL, 'TAG103')
#3 /home/dev/trunk/Zend/Mail/Storage/Imap.php(353): Zend_Mail_Protocol_Imap->fetch('UID', 12267)
#4 /home/dev/trunk/model/gmail_imap_oauth.class.php(121): Zend_Mail_Storage_Imap->getUniqueId(12267)

有没有办法让连接保持更长时间?我正在通过命令行运行此脚本并尝试在 php.ini 中增加脚本最大运行时间,但没有帮助。

【问题讨论】:

  • 我正在检查 zend 库文件 Imap.php,它使用 fsockopen 连接到 imap 服务器。可能是连接的一些超时限制。
  • 这里的代码,创建对象后,调用 indexAllMails() 开始获取。

标签: php zend-framework imap


【解决方案1】:

这里是函数

公共函数 indexAllMails($startIndex=1) {

$this->_imap = new Zend_Mail_Protocol_Imap('imap.gmail.com', '993', true);
$authenticateParams = array('XOAUTH', $initClientRequestEncoded);
$this->_imap->requestAndResponse('AUTHENTICATE', $authenticateParams);

//Create the mail storage Object
$this->_storage = new Zend_Mail_Storage_Imap_Wrapper($this->_imap);

//Select Folder
$this->_storage->selectFolder("[Gmail]/All Mail");


$numMessagesTotal = $this->_storage->countMessages();
if($numMessagesTotal == 0 ) return true;

for($i=$startIndex;$i<=$numMessagesTotal;$i++)
{
  try {
    $uniqueId = $this->_storage->getUniqueId($i);
    $message = $this->_storage->getMessage($i);
  }
  catch(Exception $ex)
  {
      log("Error getting Unique id",'index');
      log($ex->getMessage(),'index');
      log($ex->getTraceAsString(),'index');

      if($ex->getMessage() == 'cannot read - connection closed?')
      {
          //Timeout :(
          return true;
      }
      else
        continue;
  }

  $from = $message->from;
  echo $from;
}

}

【讨论】:

  • 这是服务器端断开连接(即从 gmail 断开连接),它在连接后大约 45 分钟内发生。我已经通过 java 和 python 库进行了尝试,它们的行为都相同,所以这肯定不是客户端问题。问题是,如果我立即重新连接(在断开连接时),gmail 会在 10 秒内再次断开我的连接。如果我在 10 分钟内再次尝试重新连接,那么接下来的 45 分钟可以正常工作。
  • 我必须想办法加快从 gmail 获取消息的速度并在 45 分钟内完成!现在寻找 C 库
猜你喜欢
  • 1970-01-01
  • 2011-08-17
  • 2012-05-09
  • 2022-01-21
  • 1970-01-01
  • 2011-01-31
  • 2016-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多