xml-string-streamer-guzzle
xml-string-streamer-guzzle copied to clipboard
Leveraging existing Guzzle client to create streaming requests
Hi @prewk ,
Any ideas on how to leverage an existing client to do requests? The reason I ask is that there is interest to be able to use this guzzle streamer for large XML responses that are behind a HTTP Authentication digest with a shared cookie jar.
Thoughts?
Hi!
It should actually be pretty straight-forward if you're proficient with Guzzle (which I unfortunately am not, being away from PHP for many years), this package is just a small convenience because it's a common use case.
As you can see in its source it's just one file implementing the StreamInterface: https://github.com/prewk/xml-string-streamer-guzzle/blob/master/src/XmlStringStreamer/Stream/Guzzle.php
It's not doing much. It mostly just fits Guzzle into the "getChunk" thing we need.
$this->stream = new Psr7\CachingStream(
Psr7\stream_for(fopen($url, 'r'))
);
If you change this (create a new class yourself, perhaps) into
$context = stream_context_create([
"http" => [
// ................. PHP stream context options
]
]);
$this->stream = new Psr7\CachingStream(
Psr7\stream_for(fopen($url, 'r', false, $context))
);
You can bring your own PHP stream context options.
Or better - implement Prewk\XmlStringStreamer\StreamInterface (like this package does) in a new class and bring your own Guzzle solution entirely. Here's some docs how to read a couple of bytes at a time (eg. streaming) with Guzzle: https://docs.guzzlephp.org/en/5.3/streams.html#creating-streams
If you don't need rewind support (who needs it anyway) you can probably just leave isSeekable and rewind unimplemented or throw an exception there.
@prewk thanks, I actually put in a PR to add a Guzzle\Response streamhandler. This solves my issues since it now allowed me to create whatever request I needed from my client and since $response->getBody() - returns a Stream it became ease to wrap it a StreamIterator class you had already.
I think this solves a few other closed issues in the original xml-string-streamer, since others were looking to have a way to pass in a stream that was not locked to a file I/O operation.
P.R: https://github.com/prewk/xml-string-streamer-guzzle/pull/9