dozoqn3347 2013-12-18 10:49
浏览 41

从Zendesk API获取所有门票

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条回答 默认 最新

  • dongmei9508 2014-07-02 20:17
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接