{"id":19374715,"url":"https://github.com/gyselroth/php-stream-iterator","last_synced_at":"2026-03-14T09:04:15.896Z","repository":{"id":56983628,"uuid":"154497283","full_name":"gyselroth/php-stream-iterator","owner":"gyselroth","description":"PSR-7 compatible stream wrapper for iterators","archived":false,"fork":false,"pushed_at":"2019-12-03T14:05:32.000Z","size":2962,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T19:07:51.715Z","etag":null,"topics":["php","psr-7","stream"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gyselroth.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-24T12:28:31.000Z","updated_at":"2024-10-04T13:34:18.000Z","dependencies_parsed_at":"2022-08-21T09:10:42.482Z","dependency_job_id":null,"html_url":"https://github.com/gyselroth/php-stream-iterator","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyselroth%2Fphp-stream-iterator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyselroth%2Fphp-stream-iterator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyselroth%2Fphp-stream-iterator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyselroth%2Fphp-stream-iterator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gyselroth","download_url":"https://codeload.github.com/gyselroth/php-stream-iterator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248774981,"owners_count":21159534,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","psr-7","stream"],"created_at":"2024-11-10T08:35:56.809Z","updated_at":"2026-03-14T09:04:15.870Z","avatar_url":"https://github.com/gyselroth.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-stream-iterator\n\n[![Build Status](https://travis-ci.org/gyselroth/php-stream-iterator.svg)](https://travis-ci.org/gyselroth/php-stream-iterator)\n [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/gyselroth/php-stream-iterator/master/LICENSE)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/gyselroth/php-stream-iterator/badges/quality-score.png)](https://scrutinizer-ci.com/g/gyselroth/php-stream-iterator)\n[![Code Coverage](https://scrutinizer-ci.com/g/gyselroth/php-stream-iterator/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/gyselroth/php-stream-iterator/?branch=master)\n[![GitHub release](https://img.shields.io/github/release/gyselroth/php-stream-iterator.svg)](https://github.com/gyselroth/php-stream-iterator/releases)\n[![Latest Stable Version](https://img.shields.io/packagist/v/gyselroth/stream-iterator.svg)](https://packagist.org/packages/gyselroth/stream-iterator)\n\n\\StreamIterator\\StreamIterator provides a fully PSR-7 compatible stream wrapper for an interator.\nYou may also pass a callback which handles each yielded iterator entry.\n\\StreamIterator is also nicely useable on blocking iterators and/or to create realtime stream responses.\n\n# Requirements\n\n* The minimum supported PHP version is 5.6\n* The library depends on the following external PHP libraries:\n    * psr/http-message (^1.0)\n\n# Installation\n\nThe package is available at packagist an can be installed via composer:\n\n```\ncomposer require gyselroth/stream-iterator\n```\n\n## Documentation\n\nThe examples use a simple ArrayIterator, of course you may use any kind of traversable object.\n\n### Read the whole iterator\n```php\n$my_iterator = new \\ArrayIterator([0,1,2,3,4,5]);\n$stream = new \\StreamIterator\\StreamIterator($my_iterator);\n$contents = $stream-\u003egetContents();\necho $contents; //Prints 012345\n```\n\n### Using a callback\n\nUsing a callback enables us to operate on each of the yielded iterator elements:\n```php\n$my_iterator = new \\ArrayIterator([0,1,2,3,4,5]);\n$stream = new \\StreamIterator\\StreamIterator($my_iterator, function($item) {\n    return '-'.$item;\n})\n\n$contents = $stream-\u003egetContents();\necho $contents; //Prints -0-1-2-3-4-5\n```\n\n### JSON stream example\n\nIn this example we create a json output from the example iterator:\n\n```php\n$my_iterator = new \\ArrayIterator([['foo' =\u003e 'bar'], ['foo' =\u003e 'bar']]);\n$stream = new \\StreamIterator\\StreamIterator($my_iterator, function($item) {\n    if($this-\u003etell() === 0) {\n        $string = '[';\n    } else {\n        $string = ',';\n    }\n\n    $string .= json_encode($item);\n\n    if($this-\u003eeof()) {\n        $string .= ']';\n    }\n\n    return $string;\n})\n\n$contents = $stream-\u003egetContents();\necho $contents; //Prints [{\"foo\":\"bar\"},{\"foo\":\"bar\"}]\n```\n\n### (JSON) stream without buffer\nThis enables a realtime json stream of an iterator. This also allows to operate on blocking iterators\nwhere \\Iterator::next() blocks until a new entry gets yielded. Each iterator item gets printed as soon as it arrives.\n\n\u003e**Note** Some web server have output buffers or gzip enabled, this will not work with a realtime stream. Be sure\nthat 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\nto send a header `header('X-Accel-Buffering', 'no')` to disable the fastcgi nginx buffer. Otherwise nginx will buffer your output.\n\n```php\n$my_iterator = new \\ArrayIterator([['foo' =\u003e 'bar'], ['foo' =\u003e 'bar']]);\n$stream = new \\StreamIterator\\StreamIterator($my_iterator, function($item) {\n    if($this-\u003etell() === 0) {\n        $string = '[';\n    } else {\n        $string = ',';\n    }\n\n    $string .= json_encode($item);\n\n    if($this-\u003eeof()) {\n        $string .= ']';\n    }\n\n    echo $string;\n    flush();\n    return '';\n})\n\n$contents = $stream-\u003egetContents(); //Prints [{\"foo\":\"bar\"},{\"foo\":\"bar\"}]\necho $contents; //Prints \"\" (Empty string)\n```\n\n## Changelog\nA changelog is available [here](https://github.com/gyselroth/php-stream-iterator/CHANGELOG.md).\n\n## Contribute\nWe 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).\n\n## Thanks\nThis projects use ideas provided by Matthew Weier O'Phinney [phly/psr7examples](https://github.com/phly/psr7examples).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyselroth%2Fphp-stream-iterator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgyselroth%2Fphp-stream-iterator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyselroth%2Fphp-stream-iterator/lists"}