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 | |
} |
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
Hey, I discovered this gist from the PHP sub-reddit. I'm not 100% sure if this is what you're after, but it looks like you're trying to accomplish something akin to co-routines. I wrote a PHP library for executing generator based co-routines called Recoil. It's based on React, so perhaps won't support CURL directly, but will support other methods of making asynchronous HTTP requests.