PHPackages                             gyselroth/stream-iterator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. gyselroth/stream-iterator

ActiveLibrary[HTTP &amp; Networking](/categories/http)

gyselroth/stream-iterator
=========================

Provides \\StreamIterator which allows traversing through an iterator and stringify each element

v1.0.2(6y ago)33.0k↓16.1%[1 issues](https://github.com/gyselroth/php-stream-iterator/issues)MITPHPPHP &gt;=5.6CI failing

Since Oct 25Pushed 6y ago1 watchersCompare

[ Source](https://github.com/gyselroth/php-stream-iterator)[ Packagist](https://packagist.org/packages/gyselroth/stream-iterator)[ Docs](https://github.com/gyselroth/php-stream-iterator)[ RSS](/packages/gyselroth-stream-iterator/feed)WikiDiscussions master Synced yesterday

READMEChangelog (3)Dependencies (4)Versions (4)Used By (0)

php-stream-iterator
===================

[](#php-stream-iterator)

[![Build Status](https://camo.githubusercontent.com/0bd1d125a76e34a71710f07d342b74972c6e6d6634aa7a880d76d1ca251d334d/68747470733a2f2f7472617669732d63692e6f72672f677973656c726f74682f7068702d73747265616d2d6974657261746f722e737667)](https://travis-ci.org/gyselroth/php-stream-iterator)[![GitHub license](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://raw.githubusercontent.com/gyselroth/php-stream-iterator/master/LICENSE)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/ea0039ce47aeb52833d7a33f4a0cacc3f7c556639448d4a262bf9c876ae20ec8/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f677973656c726f74682f7068702d73747265616d2d6974657261746f722f6261646765732f7175616c6974792d73636f72652e706e67)](https://scrutinizer-ci.com/g/gyselroth/php-stream-iterator)[![Code Coverage](https://camo.githubusercontent.com/2d956f11d06c4f24540589bbe7432b7606021384ddafd3a7f422fcbf1b4944f8/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f677973656c726f74682f7068702d73747265616d2d6974657261746f722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/gyselroth/php-stream-iterator/?branch=master)[![GitHub release](https://camo.githubusercontent.com/e95ef4279e38249a18687eb6187dd1791037db75e1aba3c44a3c44725bd866d4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f677973656c726f74682f7068702d73747265616d2d6974657261746f722e737667)](https://github.com/gyselroth/php-stream-iterator/releases)[![Latest Stable Version](https://camo.githubusercontent.com/23293c3fb15c3a32cf5369d4f3bbde37e5c061ee7eef3325e0045f342a7bbede/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f677973656c726f74682f73747265616d2d6974657261746f722e737667)](https://packagist.org/packages/gyselroth/stream-iterator)

\\StreamIterator\\StreamIterator provides a fully PSR-7 compatible stream wrapper for an interator. You may also pass a callback which handles each yielded iterator entry. \\StreamIterator is also nicely useable on blocking iterators and/or to create realtime stream responses.

Requirements
============

[](#requirements)

- The minimum supported PHP version is 5.6
- The library depends on the following external PHP libraries:
    - psr/http-message (^1.0)

Installation
============

[](#installation)

The package is available at packagist an can be installed via composer:

```
composer require gyselroth/stream-iterator

```

Documentation
-------------

[](#documentation)

The examples use a simple ArrayIterator, of course you may use any kind of traversable object.

### Read the whole iterator

[](#read-the-whole-iterator)

```
$my_iterator = new \ArrayIterator([0,1,2,3,4,5]);
$stream = new \StreamIterator\StreamIterator($my_iterator);
$contents = $stream->getContents();
echo $contents; //Prints 012345
```

### Using a callback

[](#using-a-callback)

Using a callback enables us to operate on each of the yielded iterator elements:

```
$my_iterator = new \ArrayIterator([0,1,2,3,4,5]);
$stream = new \StreamIterator\StreamIterator($my_iterator, function($item) {
    return '-'.$item;
})

$contents = $stream->getContents();
echo $contents; //Prints -0-1-2-3-4-5
```

### JSON stream example

[](#json-stream-example)

In this example we create a json output from the example iterator:

```
$my_iterator = new \ArrayIterator([['foo' => 'bar'], ['foo' => 'bar']]);
$stream = new \StreamIterator\StreamIterator($my_iterator, function($item) {
    if($this->tell() === 0) {
        $string = '[';
    } else {
        $string = ',';
    }

    $string .= json_encode($item);

    if($this->eof()) {
        $string .= ']';
    }

    return $string;
})

$contents = $stream->getContents();
echo $contents; //Prints [{"foo":"bar"},{"foo":"bar"}]
```

### (JSON) stream without buffer

[](#json-stream-without-buffer)

This enables a realtime json stream of an iterator. This also allows to operate on blocking iterators where \\Iterator::next() blocks until a new entry gets yielded. Each iterator item gets printed as soon as it arrives.

> **Note** Some web server have output buffers or gzip enabled, this will not work with a realtime stream. Be sure that all buffers are completely disabled (For endpoints where a realtime stream is used). For example if you are using Nginx and PHP-FPM you will most likely need to send a header `header('X-Accel-Buffering', 'no')` to disable the fastcgi nginx buffer. Otherwise nginx will buffer your output.

```
$my_iterator = new \ArrayIterator([['foo' => 'bar'], ['foo' => 'bar']]);
$stream = new \StreamIterator\StreamIterator($my_iterator, function($item) {
    if($this->tell() === 0) {
        $string = '[';
    } else {
        $string = ',';
    }

    $string .= json_encode($item);

    if($this->eof()) {
        $string .= ']';
    }

    echo $string;
    flush();
    return '';
})

$contents = $stream->getContents(); //Prints [{"foo":"bar"},{"foo":"bar"}]
echo $contents; //Prints "" (Empty string)
```

Changelog
---------

[](#changelog)

A changelog is available [here](https://github.com/gyselroth/php-stream-iterator/CHANGELOG.md).

Contribute
----------

[](#contribute)

We are glad that you would like to contribute to this project. Please follow the given [terms](https://github.com/gyselroth/php-stream-iterator/blob/master/CONTRIBUTING.md).

Thanks
------

[](#thanks)

This projects use ideas provided by Matthew Weier O'Phinney [phly/psr7examples](https://github.com/phly/psr7examples).

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance13

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~202 days

Total

3

Last Release

2404d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/29914568?v=4)[Sandro Aebischer](/maintainers/s-aebischer)[@s-aebischer](https://github.com/s-aebischer)

![](https://www.gravatar.com/avatar/c5faa1a8f30f97714249f80b7594e11976f9740912259431764cf0a622958d05?d=identicon)[danielkleeb](/maintainers/danielkleeb)

---

Top Contributors

[![raffis](https://avatars.githubusercontent.com/u/2376735?v=4)](https://github.com/raffis "raffis (14 commits)")

---

Tags

phppsr-7streampsr-7streamiterator

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/gyselroth-stream-iterator/health.svg)

```
[![Health](https://phpackages.com/badges/gyselroth-stream-iterator/health.svg)](https://phpackages.com/packages/gyselroth-stream-iterator)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.1B4.0k](/packages/guzzlehttp-psr7)[symfony/psr-http-message-bridge

PSR HTTP message bridge

1.3k320.9M983](/packages/symfony-psr-http-message-bridge)[mezzio/mezzio

PSR-15 Middleware Microframework

3923.8M125](/packages/mezzio-mezzio)[telnyx/telnyx-php

Official Telnyx PHP SDK — APIs for Voice, SMS, MMS, WhatsApp, Fax, SIP Trunking, Wireless IoT, Call Control, and more. Build global communications on Telnyx's private carrier-grade network.

35789.4k2](/packages/telnyx-telnyx-php)[sunrise/http-router

A powerful solution as the foundation of your project.

17451.6k10](/packages/sunrise-http-router)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28150.5k](/packages/phpro-http-tools)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
