Last active
July 11, 2018 01:31
-
-
Save magnetikonline/fc6f7e99ccfe80810cda to your computer and use it in GitHub Desktop.
Unicorns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TestClass { | |
public function generator() { | |
$this->curlRequestThingy( | |
'http://domain.com/endpoint', | |
// function here passed to CURLOPT_WRITEFUNCTION | |
function($data) { | |
// how to yield back to generator() method? | |
yield $data; | |
// return bytes written (obviously this does NOT work - but is required by Curl) | |
return strlen($data); | |
} | |
); | |
} | |
// more code here.... | |
} | |
// for example | |
foreach ((new TestClass())->generator() as $dataBlob) { | |
// do something with $dataBlob | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd be tempted to just use fsockopen and talk HTTP directly to the endpoint. Read up to the
\r\n\r\n
at the end of the header block, at which point you've just got a stream for the body that you can do with as you wish. Wrapping that in a generator would be trivial.(I've always been surprised that there isn't an easy way to do that with PHP streams. I ended up writing something similar to manipulate S3/HTTP as memory-efficient streams here https://github.com/99designs/facade)