發送http回應後繼續處理php
P粉029327711
P粉029327711 2023-08-27 20:36:33
0
1
549
<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粉029327711
P粉029327711

全部回覆(1)
P粉351138462

是的。你可以這樣做:

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!