Search code examples
phppaginationlimitoffset

How would I loop through this API data


I have an API I'm requesting data from.

The thing is I don't want to stress my application since the data set can contain tens of thousands of objects.

The way the data is returned is like this where I can specify a page limit and an offset, like limit=10 and offset=10:

"objects": {
    "object",
    "object2",
    //and so on....
}
"hasMore": true,
"count": 23123,

The count property holds the total amount of objects, and hasMore equals true if there's more objects to fetch and false if no objects left.

If hasMore equals true my assumption is that I need to make a new request with the offset equal to double my pagesize until hasMore equals false, right?

I've tried something like this, but no work since I'm left with an almost empty object with only the hasMore equal false and total count:

 $pageSize = 10;
 $pages = ceil(23123 / $pageSize); // total count of objects
 for ($page = 0; $page <= $pages; $page++) {
    $products = $api->get_products($page * $pageSize, $pageSize);
    var_dump(json_decode($products));
 }

Solution

  • Concur with @miken32

    Assuming it's $api->getproducts( $offset, $limit ), try this:

    $pageSize      = 10;
    $currentPage   = 0;
    $hasmore       = true;
    
    while( $hasmore === true ) {
        $currentOffset = $currentPage * $pageSize;
        $products      = json_decode( $api->get_products( $currentOffset, $pageSize ), true );
        $hasmore       = $products['hasMore'];
        processProducts( $products['objects'] );
        $currentPage++;
    }