I am unable to get fopen to read content stream in PHP. There is no error thrown, the content is always empty string.
Here is my code:
$contents='';
$handle = fopen("php://stdin", "r") or error_log('got some error');
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
error_log($contents);
Then from Postman I send POST request at my server url like
POST http://myserverurl/index.php
I do send post data on above request, I have tried form-encoded, binary and raw too.
But error_log logs empty string to the log file, which means that fopen did actually work but it got empty content.
I have checked php.ini settings as well and allow_url_fopen setting is true as well.
I am testing this on nginx with PHP7.1-fpm. I recently switched from Apache and PHP 5.6 to nginx and php7.1-fpm and it stopped working.
It was totally working fine with Apache and PHP5.6.
What is wrong here? Any help will be highly appreciated.
There is probably some misunderstanding in the default streams of PHP.
If you're running the PHP script as a command, the CLI interface would give you php://stdin.
If you're running the PHP script in a web server / cgi / php-fpm environment, there is no php://stdin
at all. You'll be getting php://input stream (for the POST body) instead.
Reference: Supported Protocol and Wrapper