Get pagination results in Active Collab API

和自甴很熟 提交于 2019-11-29 15:30:56

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!