1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How would I loop through this API data

Discussion in 'Programming/Internet' started by Dueify, Sep 12, 2018.

  1. Dueify

    Dueify Guest

    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": {
    //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);

    Login To add answer/comment

Share This Page