PHP - imap_ping() 函式



PHP−IMAP 函式可幫助您訪問電子郵件帳戶,IMAP 代表 **I**nternet **M**ail **A**ccess **P**rotocol,使用這些函式,您還可以使用 NNTP、POP3 協議和本地郵箱訪問方法。

**imap_ping()** 函式接受一個表示 IMAP 流的資源值作為引數,並驗證給定的流是否仍然處於活動狀態。

語法

imap_ping($imap_stream);

引數

序號 引數及描述
1

imap_stream (必填)

這是一個表示 IMAP 流的字串值,是 **imap_open()** 函式的返回值。

返回值

如果給定的流仍然處於活動狀態,則此函式返回布林值 TRUE,否則返回 FALSE。

PHP 版本

此函式首次引入於 PHP 4 版本,並在所有後續版本中均有效。

示例

以下示例演示了 **imap_ping()** 函式的使用方法:

<html>
   <body>
      <?php
         //Establishing connection
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         $imap = imap_open($url, $id, $pwd);
         print("Connection established...."."<br>");
		   
         //list of mailboxes
         $res = imap_ping($imap);
	      
         if($res){
            print("IMAP stream is alive");		 
         } else {
            print("IMAP stream is not alive");		 
         }
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....
IMAP stream is alive

示例

以下是此函式的另一個示例:

<html>
   <body>
      <?php
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         $mailbox = imap_open($url, $id, $pwd);
         print("Connection established....");

         if (imap_ping($mailbox)) {
            //Retrieving the contents of mail boxes
            $list = imap_getmailboxes($mailbox, $url, "*");
            if (is_array($list)) {
              foreach ($list as $key => $val) {
                  print_r($val);
                  print("<br>");
               }
            }else{  }
         }
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....stdClass Object ( [name] => 
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX [attributes] => 64 
[delimiter] => / )
stdClass Object ( [name] => 
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.changed_mail1 [attributes] =
> 64 [delimiter] => / )
stdClass Object ( [name] => 
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.changed_mail2 [attributes] =
> 64 [delimiter] => / )
php_function_reference.htm
廣告

© . All rights reserved.