guzzle/guzzle — pull request #254 by mtdowling (2013-03-04 04:11:49+00:00)
Title: Streaming responses?

I've been investigating ways that I can add streaming responses to Guzzle so that it can working with streaming APIs like Twitter, force.com, etc.

**Note: I'm looking for advice on this PR. This PR isn't quite done.**

This PR adds a StreamRequestFactoryInterface object that converts a Guzzle\Http\Message\Request object into a PHP stream that uses the http or https stream wrappers. This allows you to pull off of the stream as needed and arbitrarily close the stream as needed.

```php
get('https://stream.twitter.com/1/statuses/sample.json');
$request->setAuth('my_username', 'my_password);

$factory = new PhpStreamRequestFactory();
$fp = $factory->fromRequest($request);

$c = 0;
while (!feof($fp)) {
var_export(json_decode(fgets($fp), true));
// Close the stream after getting 100 updates
if (++$c > 100) {
break;
}
}

fclose($fp);
```

There are some issues with this implementation in that it does not currently trigger most of the lifecycle events used when sending during the normal CurlMulti process (e.g. request.before_send, request.error, request.exception, request.sent, request.complete).

There are probably other cURL specific options that can be carried over as context options for the stream (e.g. timeouts).

An alternative to this approach would be to implement a stream wrapper the utilizes Guzzle's CurlMulti object and would allow you to streaming read off of the connection. This would require a little bit of refactoring to CurlMulti and possibly a subclass.