I have to write something to process an XML document sent via POST. The document has base-64 encoded binaries inside so the request can be quite large.
This works:
$document = simplexml_load_file('php://input');
But I am using the Zend Diactoros PSR-7 implementation so really I should be doing something like this:
$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
$document = simplexml_load_file($request->getBody());
However, that causes the stream to be cast to a string which results in an error.
What I really need is something like:
$document = simplexml_load_file($request->getBody()->stream);
Because:
var_dump($request->getBody());
object(Zend\Diactoros\PhpInputStream)#5 (4) {
["cache":"Zend\Diactoros\PhpInputStream":private] => string(0) ""
["reachedEof":"Zend\Diactoros\PhpInputStream":private ] => bool(false)
["resource":protected] => resource(4) of type (stream)
["stream":protected] => string(11) "php://input"
}
But note ->stream
is protected. Should I just extend Zend\Diactoros\PhpInputStream
and write a public method to expose ->stream
? Or is there a better way?
Please note: I am looking for a stream based solution; not to read the entire stream into memory as a string.