Search code examples
phpapiactivecollab

Get pagination results in 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!


Solution

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