在Active Collab API中获取分页结果

在Active Collab API中获取分页结果

问题描述:

I have just discovered you can get pagination results through the api by passing in the page parameter like so:

$projects = $client->get('projects/147/time-records?page=3')->getJson();

Is there a way of knowing how many time records a project has so I know how many times I need to paginate?

Alternatively, how would I go about retrieving several pages worth of data - i'm struggling with the code!

I have created an issue on Github - will await a response.

For now, I do the following:

// Get all the projects

// Set the page number
$page = 1;

// Create an empty array
$project_records = array();

// Get the first page of results
$project_records_results = $client->get('projects?page=' . $page)->getJson();

// Merge the results with base array
$project_records = array_merge($project_records, $project_records_results);

// Get the next page of results, 
// if it returns something merge with the base array and continue
while ($project_records_results = $client->get('projects?page=' . ++$page)->getJson()) {
    $project_records = array_merge($project_records, $project_records_results);
}

Sure. All paginated results will include following headers:

  • X-Angie-PaginationCurrentPage - indicates current page
  • X-Angie-PaginationItemsPerPage - indicates number of items per page
  • X-Angie-PaginationTotalItems - indicates number of items in the entire data set.

When you get header values, simple:

$total_pages = ceil($total_items_header_value / $items_per_page_header_value);

will give you number of pages that are in the collection.

Alternative: You can iterate through pages (by starting with page GET parameter set to 1, and incrementing it) until you get an empty result (page with no records). Page that returns no records is the last page.