2012-03-30 146 views
0

我正在建立一個基於聊天/短信的系統我有它發出的消息,但我需要做的是實時更新傳入電子郵件中的數據,而無需用戶重新加載(阿賈克斯?)我需要從主頁面傳遞一個數字到fetch.php,它可以獲取電子郵件,並創建一個沒有被閱讀並且來自正確發送者的電子郵件的數組,我需要做的事情是將數字從主頁面發送到抓取頁面並返回一組新的消息到主textarea,但是我在ajax上找到的所有教程似乎都需要一個數據庫,並且我不知道如何在延遲幫助中運行和返回數據,我們將不勝感激。基於電子郵件的PHP聊天窗口

這裏是fetch.php的內容:

<?php 

error_reporting(E_ALL); 
ini_set('display_errors', TRUE); 
ini_set('display_startup_errors', TRUE); 

/* connect to gmail */ 
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
$username = '[email protected]'; 
$password = 'passwd'; 

/* try to connect */ 
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); 

/* grab emails */ 
$emails = imap_search($inbox,'ALL'); 

/* if emails are returned, cycle through each... */ 
if($emails) { 

    $messages[] = ''; 

    /* begin output var */ 
    $output = ''; 

    /* put the newest emails on top */ 
    rsort($emails); 

    /* for every email... */ 
    foreach($emails as $email_number) { 

    /* get information specific to this email */ 
    $overview = imap_fetch_overview($inbox,$email_number,0); 
    $message = imap_fetchbody($inbox,$email_number,1); 

    //print_r($overview); 

    $Is_sms = strpos($overview[0]->from, "txt.voice.google.com"); 
    if($Is_sms === false) continue; 
    if($overview[0]->seen != 0) continue; 

    $pnl = strpos($overview[0]->from, "."); 
    $pnumber = substr($overview[0]->from, $pnl +2, 10); 

    if($pnumber != "3303331866") continue; 

    $messages[] = $message; 

     //$status = imap_setflag_full($mbox, $mail, "\\Seen \\Flagged", ST_UID); 
    /* output the email header information */ 
    /*$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">'; 
    $output.= '<span class="subject">'.$overview[0]->subject.'</span> '; 
    $output.= '<span class="from">'.$overview[0]->from.'</span>'; 
    //$output.= '<span class="date">on '.$overview[0]->date.'</span>'; 
    $output.= '<span class="pnumber">'.$pnumber.'</span>'; 
    $output.= '</div>';*/ 

    /* output the email body */ 
    //$output.= '<div class="body">'.$message.'</div>'; 
    } 

    //echo $output; 
    print_r($messages); 
} 

/* close the connection */ 
imap_close($inbox); 

主要就是頁面只是一個號文本,內容文本區域,消息文本框,併發送按鈕。

回答

0

當瀏覽器的AJAX請求需要時,需要數據庫存儲消息供您稍後獲取。這是因爲您的PHP腳本不會在兩次後續運行之間保留其變量,因此您需要將消息存儲在某處。因此,它基本上會是這樣的:

  1. 獲取新信息
  2. 將其放置在某種有序的隊列,在那裏你可以晚於哪些客戶端已經有獲取一切。
  3. 發送給客戶。

該隊列可以用從第一個帖子向上計數的數字進行編號,從而允許客戶端指定他們得到的最後收到的消息是什麼並且請求一切更新。順便說一下,如果我沒有在上面說得足夠清楚,那麼你對AJAX要求數據庫的說法有些誤解。如果沒有,AJAX可以正常工作,它只是你的情況,需要一些存儲和數據庫是一個很好的方法來做到這一點。

希望能幫到:)