title: Best way of handling pagination in a PHP client library
body: <p>Assume I have a REST API that supports pagination (and filtering). Upon executing a GET request to myapi.com/api/users, it returns something like this:</p>
  
  <pre><code>{
    total: 100
    data: . . .
    first_page_url: "/?page=1"
    from: 0 
    next_page_url: "/?page=2"
    path: "/"
    per_page: 10
    prev_page_url: null
    to: 10
  }
  </code></pre>
  
  <p>And then in my PHP client library I have something similar to this for a function that performs this request and returns the JSON response as an associative array:</p>
  
  <pre><code>public function users($page = 1, $verified = null, $admin = null, $joined = null, $active_until = null, $banned_until = null, $referral_code = null) {
      . . .
  }
  </code></pre>
  
  <p>What would be the best way to fetch the next result?
  I am envisioning something like this:</p>
  
  <pre><code>$users = [];
  $request = users(); // gets the first page
  while ($request) {
      $request = next_users();
      array_push($users, $request['data']);
  }
  </code></pre>
  
  <p>The problem is that I have no idea where to begin when it comes to putting the result of users() into next_users() while preserving any filter values (the optional parameters of users()), all while doing it elegantly. I have tried looking around, but I can't find many results of API clients handling pagination. As demonstrated above, the ideal scenario would be having the ability to keep getting the next page of results until there are none left with next_users().</p>