I’m creating an API, that retrieves items from a third party component and returns these in a specified XML/CSV/TEXT structure, that can be customized by the admin via a template.
The problem: One API-request may easily include millions of items. So it’s memory-wise not possible to create the whole list server side and send it to the client.
Instead the items should be created on-the-fly and the results should be sent to the client immediately, without storing them in PHP’s memory.
How it this possible?
Example template:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<items>
{items}
<item no="{number}">{item}</item>
{/items}
</items>
Current code example without streaming. Not actually working, but you should get die idea:
echo preg_replace_callback('@{items}(.*){/items}@si', function (array $matches)
{
return createItems($matches[1]);
}, $template);
function createItems($itemTemplate)
{
$items = '';
while (itemsExist()) {
$items .= getItem($itemTemplate);
}
}
I guess, I should stop buffering each item in a var and instead echo them directly? But how do I keep the XML’s/CSV’s/JSON’s structure intact or whatever else is in the template around the list?