Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active July 11, 2018 01:31
Show Gist options
  • Save magnetikonline/fc6f7e99ccfe80810cda to your computer and use it in GitHub Desktop.
Save magnetikonline/fc6f7e99ccfe80810cda to your computer and use it in GitHub Desktop.
Unicorns.
<?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
}
@lox
Copy link

lox commented Jul 8, 2014

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment