Getting all tickets from Zendesk API

5.9k views Asked by At

I am trying to get all ticket data from Zendesk using their v2 API - the API is limited to 100 responses per page and then paginated but I cannot seem to cycle through all the pages to get the total number of tickets (and other information)

As I have no idea how many pages there will be I am stuggling to set a loop to deal with this efficiently. At the moment I have used 20 as I know I have less than 2000 tickets:

for ($page=1;$page<20;$page++) {
    if ($page==1) {
        $data = curlWrap("/ticket_metrics.json", null, "GET");
    } else {
        if ($data->next_page!="NULL"&&$data->next_page!=""&&$data->next_page!=0) {
            $data = curlWrap("/ticket_metrics.json?page=$page", null, "GET");
        }
    }

    foreach ($data as $ticket) {
        if (is_array($ticket)) {
            foreach ($ticket as $i) {
                $time=$i->reply_time_in_minutes->calendar;
                if ($time!=0 &&$time!="") {
                    $total_time=$total_time+$time;
                    $counter++;
                }
            }
        }
    }
}



$answer=$total_time/$counter;

print $total_time."/".$counter."=".$answer;

How can I get the total number of tickets and the first response time so I can perform this calculation?

Thanks

1

There are 1 answers

0
osowskit On

Not sure if you still need it but will post in case others have this problem.

The number of tickets is returned in the response as 'count'. You are also correct that a maximum of 100 records are returned per page - though you can return less. This page for reference : http://developer.zendesk.com/documentation/rest_api/introduction.html

I'd refactor the code to use a while loop until the next_page is 'null' (i think the case was your problem).

$page_num = '1';
$total_records = -1;
while ($page != 'null')
{
    //query API return to $data
    $url = "/ticket_metrics.json?page=" . $page_num;
    $data = curlWrap($url, null, "GET");  

    //'count' is returned in json
    if ($total_records < 0)
        $total_records = $data->$count; 
    $page = $data->next_page;

    //add total_time and increase your counter
}
//process your data