PHP超时从中读取PUT数据php://input
我创建了一个Restful API。 我可以从中读取PUT数据php://input,但我无法为此操作设置超时。如果读取PUT数据需要1秒以上,则应退出以下脚本。我还尝试了不同的方法,如“file_get_content”、“stream_get_contents”或为“max_input_time”设置不同的值,但如果脚本需要的时间比我设置的时间长,脚本不会停止PHP超时从中读取PUT数据php://input,php,rest,timeout,put,Php,Rest,Timeout,Put,我创建了一个Restful API。 我可以从中读取PUT数据php://input,但我无法为此操作设置超时。如果读取PUT数据需要1秒以上,则应退出以下脚本。我还尝试了不同的方法,如“file_get_content”、“stream_get_contents”或为“max_input_time”设置不同的值,但如果脚本需要的时间比我设置的时间长,脚本不会停止 $handle = fopen("php://input", "r"); $timeout = 1; stream_set_tim
$handle = fopen("php://input", "r");
$timeout = 1;
stream_set_timeout($handle, $timeout);
stream_set_blocking($handle, true);
$info = stream_get_meta_data($handle);
$putData = "";
while (!feof($handle)) {
if (($putData .= fgets($handle, 4096)) === false) {
$info = stream_get_meta_data($handle);
}
}
fclose($handle);
if ($info['timed_out']) {
throw new \Exception("timeout");
}
不,我也读过这篇文章。我的最新代码与本文答案中的代码几乎没有什么不同。