Get pagination results in Active Collab API

ぐ巨炮叔叔 提交于 2019-11-28 09:06:46

问题


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!


回答1:


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);
}



回答2:


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.



来源:https://stackoverflow.com/questions/40020003/get-pagination-results-in-active-collab-api

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