简体   繁体   中英

Alternative way to read raw I/O stream in PHP

I am trying to find an alternative to reading php://input. I use this for getting XML data from a CURL PUT.

I usually do this with:

$xml = file_get_contents('php://input');

However, I'm having a few issues with file_get_contents() on Windows.

Is there an alternative, perhaps using fopen() or fread() ?

Yes, you can do:

$f = fopen('php://input', 'r');
if (!$f)  die("Couldn't open input stream\n");
$data = '';
while ($buffer =  fread($f, 8192)) $data .= $buffer;
fclose($f);

But, the question you have to ask yourself is why isn't file_get_contents working on windows? Because if it's not working, I doubt fopen would work for the same stream...

Ok. I think I've found a solution.

$f = @fopen("php://input", "r");
$file_data_str = stream_get_contents($f);
fclose($f);

Plus, with this, I'm not mandated to put in a file size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM