I'm reading POST data from php://input
with file_get_contents
but I have noticed situations that some of my clients don't send proper header information, i.e. the Content-Length
header is longer than the actual length of the content send. This caused file_get_contents
to wait taking up precious Apache threads.
I was able to simulate the issue with the following code:
<?php
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 1,
)
) );
$input = file_get_contents('php://input', 0, $ctx);
print_r( $input );
And calling the script with the following test command:
time curl -H 'Content-Type: application/json' -H 'Content-Length: 100' -X POST --verbose -k -d 'This is test data.' http://localhost/form.php
As you might notice, I'm setting the Content-Length
to 100 while the actual test data length is only 18.
I have tried setting an timeout by using a context on file_get_contents
but for some reason this is not taken into account.
How can I make file_get_contents
timeout in a reasonable amount of time, say 1 or 2 seconds?