This is more a theoretical question, because I want to implement a way to deal with big arrays of objects that need to be converted to json using json_encode
and I want to avoid having the whole array of results in memory to convert the whole thing (it could lead into memory issues).
What I want to do is having a PSR-7 body stream that internally (somehow) fetches, converts and outputs one result at a time. The problem about doing this is that I don't know beforehand the final response Content-Length
.
Someone has faced the same problem? How can I compute/add that header without proxying that to an intermediate service that would add/fix the Content-Length
header?
Thanks
UPDATE Possible solution: chunked responses.
I think I found a possible solution for this, and it is using chunked responses. I still need to figure out how to use that with JSON responses and what the API clients should change in order to support them.
HTTP POST: content-length header required?
Anyway if someone has faced the same problem and has a PSR-7 compatible solution I would like to know about that.