首頁  >  問答  >  主體

發送http回應後繼續處理php

<p>我的腳本被伺服器呼叫。我會從伺服器接收 <code>ID_OF_MESSAGE</code> 和 <code>TEXT_OF_MESSAGE</code>。 </p> <p>在我的腳本中,我將處理傳入的文字並使用參數產生回應:<code>ANSWER_TO_ID</code> 和 <code>RESPONSE_MESSAGE</code>。 </p> <p>問題是我正在向傳入的<code>“ID_OF_MESSAGE”</code>發送回應,但是向我發送訊息以進行處理的伺服器會將其訊息設定為已傳遞給我(這意味著我可以將他的回應傳送到該ID),收到http 回應200 後。 </p> <p>解決方案之一是將訊息保存到資料庫並建立一些每分鐘運行一次的 cron,但我需要立即產生回應訊息。 </p> <p>是否有一些解決方案如何發送到伺服器http回應200並繼續執行php腳本? </p> <p>非常感謝</p>
P粉029327711P粉029327711391 天前550

全部回覆(1)我來回復

  • P粉351138462

    P粉3511384622023-08-28 17:34:11

    是的。你可以這樣做:

    ignore_user_abort(true);//not required
    set_time_limit(0);
    
    ob_start();
    // do initial processing here
    echo $response; // send the response
    header('Connection: close');
    header('Content-Length: '.ob_get_length());
    ob_end_flush();
    @ob_flush();
    flush();
    fastcgi_finish_request();//required for PHP-FPM (PHP > 5.3.3)
    
    // now the request is sent to the browser, but the script is still running
    // so, you can continue...
    
    die(); //a must especially if set_time_limit=0 is used and the task ends
    

    回覆
    0
  • 取消回覆