當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP imap_ping()用法及代碼示例


PHP-IMAP 函數可幫助您訪問電子郵件帳戶,IMAP 代表I互聯網M艾爾A訪問權限Protocol 使用這些函數您還可以使用 NNTP、POP3 協議和本地郵箱訪問方法。

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

用法

imap_ping($imap_stream);

參數

Sr.No 參數及說明
1

imap_stream (Mandatory)

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

返回值

如果給定的流仍然有效,則此函數返回一個布爾值,否則返回 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 - imap_ping() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。