简体   繁体   中英

Pass stream wrapped by Zend Diactoros PhpInputStream (PSR-7 StreamInterface) to fopen-like function?

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.

I asked on the GitHub repo. The solution is to detach the the stream resource from the wrapper object. You can no longer use the wrapper but at least the stream resource is summoned from the framework so at least some decoupling is achieved.

$request  = Zend\Diactoros\ServerRequestFactory::fromGlobals();
$stream   = $request->getBody()->detach();
$metadata = stream_get_meta_data($stream);
$document = simplexml_load_file($metadata['uri']); // "php://input" is passed

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