【问题标题】:Twilio API has slow response when getting conferencesTwilio API 在获取会议时响应缓慢
【发布时间】:2026-01-08 10:15:01
【问题描述】:

我不知道是我做错了什么,还是 Twilio 在请求记录方面天生就很慢。当使用 php 请求会议列表时,响应可能需要 5-7 分钟,这对于任何类型的应用程序似乎都不可行。

这是我正在使用的代码示例:

$conferences = $client->account->conferences->getIterator(0, 50, array(
    ));
    foreach ($conferences as $conference) {
        $conferenceRoom = $client->account->conferences->get($conference->sid);
        $date1 = new DateTime($conference->date_created);
        $date2 = new DateTime($conference->date_updated);
        $interval = $date1->diff($date2);
        $page = $conferenceRoom->participants->getPage(0, 50);
        $participants = $page->participants;
        $participantCount = count($participants);
        $result['conferences'][$conference->sid]['friendly_name'] = $conference->friendly_name;
        $result['conferences'][$conference->sid]['sid']           = $conference->sid;
        $result['conferences'][$conference->sid]['participants']  = $participantCount;
        $result['conferences'][$conference->sid]['status']        = $conference->status;
        $result['conferences'][$conference->sid]['duration']      = $interval->format('%H:%I:%S%');
        $result['conferences'][$conference->sid]['date_created']  = strtotime($conference->date_created);
        $result['conferences'][$conference->sid]['date']          = $conference->date_created;
}
echo json_encode($result);

【问题讨论】:

    标签: php twilio conference twilio-php


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    您正在对 PHP 库中的会议资源使用 getIterator 方法。 getIterator 返回一个为您处理分页的交互器,因此只要您继续循环遍历它,它将继续从 Twilio API 请求页面。我的猜测是你开了很多会议,所以你一直在寻呼。

    此外,对于每个会议,您都需要对会议资源进行一次 API 调用,并为通话中的参与者进行一次 API 调用。即使您的帐户中只有 10 个会议,您也将进行 30 个 API 调用。

    因此,虽然 Twilio 不会花费 5-7 分钟来返回一个响应,但您的脚本正在使用需要很长时间的 API 执行大量工作。

    我同意 Half Crazed(顺便说一句,名字很好)关于缓存结果的建议。您可以通过设置event callback URL for your calls 来更新您自己系统中的会议对象,以便 Twilio 可以向您发送有关电话会议的状态更新,并且您可以在那时保存有关它的详细信息。

    【讨论】:

    • 很酷,谢谢!我在会议上设置了一个事件回调 URL,以将信息存储到数据库中。并创建了一个较小的单独 API 调用来检索进行中的会议
    【解决方案2】:

    虽然我认为代码没有任何问题,但在处理 API 时,通常最好有一个单独的服务(例如 CRON 作业或单独的线程)来调用 API 并缓存结果。然后,您的实时应用程序将从缓存的结果中读取......这样您的网站速度就不会受到第 3 方网站的影响。显示一个小注释可能是个好主意,类似于“Results last updated on XYZ”

    加载缓慢的页面不好的原因有很多。

    【讨论】: