我刚刚发现你可以通过api传递页面参数来获得分页结果:
$projects = $client->get('projects/147/time-records?page=3')->getJson();
有没有办法知道项目有多少时间记录,所以我知道需要分页多少次?
或者,我将如何检索几页数据 - 我正在努力解决这些问题!
答案 0 :(得分:2)
我在Github上创建了一个问题 - 等待回复。
现在,我执行以下操作:
// 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);
}
答案 1 :(得分:1)
不确定。所有分页结果都将包含以下标题:
X-Angie-PaginationCurrentPage
- 表示当前页面X-Angie-PaginationItemsPerPage
- 表示每页的项目数X-Angie-PaginationTotalItems
- 表示整个数据集中的项目数。获得标题值时,简单:
$total_pages = ceil($total_items_header_value / $items_per_page_header_value);
将为您提供集合中的页数。
备选方案:您可以遍历页面(通过将page
GET参数设置为1
开始,然后递增它),直到获得空结果(没有页面)记录)。返回没有记录的页面是最后一页。
答案 2 :(得分:0)
请注意,标题现在全部为小写(v1)! 因此,以上答案应予以纠正。
要给他们打电话:
$headers = $client->get($path)->getHeaders();
/ api / v1 /中的工作代码示例:
$paginationCurrentPage = isset($headers['x-angie-paginationcurrentpage'][0]) ? $headers['x-angie-paginationcurrentpage'][0] : NULL;
$paginationItemsPerPage = isset($headers['x-angie-paginationitemsperpage'][0]) ? $headers['x-angie-paginationitemsperpage'][0] : NULL;
$paginationTotalItems = isset($headers['x-angie-paginationtotalitems'][0]) ? $headers['x-angie-paginationtotalitems'][0] : NULL;