PHP產生chunked訊號 ~ PHP SERVER send chunked

PHP產生chunked訊號 ~ PHP SERVER send chunked

PHP產生chunked訊號 ~ PHP SERVER send chunked 



GOOGLE關鍵字搜尋: php chunked response


參考資料:

https://inchoo.net/dev-talk/chunked-transfer-encoding/
https://blog.longwin.com.tw/2007/05/json_header_2007/
https://www.php.net/manual/en/function.usleep.php


GITHUB: https://github.com/jash-git/PHP-SERVER-send-chunked-


完整程式碼:

<?php
//GOOGLE: php chunked response(SERVER send chunked)
header('Content-Encoding', 'chunked');
header('Transfer-Encoding', 'chunked');
header('Content-Type: application/json; charset=utf-8');// https://blog.longwin.com.tw/2007/05/json_header_2007/
//header('Content-Type', 'text/html');
header('Connection', 'keep-alive');

//flush();
//ob_flush();


set_time_limit(0);

/*
//https://inchoo.net/dev-talk/chunked-transfer-encoding/
for ($i = 0; $i < 1000; $i++) {
$data = '{"message":"One chunked result."}';
echo strlen($data)."\r\n";
echo $data."\r\n";
  flush();
  ob_flush();
  sleep(2);
}
*/
while (true) {
	$data = '{"message":"One chunked result."}';
	//echo strlen($data)."\r\n";
	echo $data."\r\n";
    flush();
	ob_flush();
	usleep(1000000);//1sec //https://www.php.net/manual/en/function.usleep.php
	
}


?>

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *