获取每个通话记录的twilio录音

时间:2016-11-16 15:05:56

标签: twilio twilio-php twilio-api

看起来所有指南都已经过时了,twilio的网站对此没有明确的答案。

我试图获取所有呼叫的列表,并且对于每个呼叫记录检查记录记录,如果它有记录记录,则获取它的uri。

虽然我不认为这是正确的做法,我试图做的脚本非常慢,并且没有按预期工作,现在就是我现在的地方:

// Set our AccountSid and AuthToken
$sid = 'MY_SID';
$token = 'MY_TOKEN';

// Your Account Sid and Auth Token from twilio.com/user/account
$client = new Client($sid, $token );

// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls->read() as $call
) {
    echo $call->sid.",  &nbsp; ".getRecording($call->sid)."<br/>";


}

function getRecording($callsid){


    // Set our AccountSid and AuthToken
    $sid = 'MY_SID';
    $token = 'MY_TOKEN';
    $client = new Client($sid, $token);

    // Loop over the list of recordings and echo a property for each one
    foreach ($client->account->recordings->read( array( "CallSid" => $callsid )) as $recording ) {

        return "&nbsp; &nbsp; ->".$callsid." <a href='http://api.twilio.com".$recording->uri."'>Audio</a> ";

    }
}

输出结果是每个记录的URI都相同。

CAb5323eed7ed4f82b3990830777c02684,       ->CAb5323eed7ed4f82b3990830777c02684 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a> 
CA57df3525265949c4dfcaa9073b02880a,       ->CA57df3525265949c4dfcaa9073b02880a <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a> 
CA31f0ac07483d72a56d424b55672a61ab,       ->CA31f0ac07483d72a56d424b55672a61ab <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a> 
CAac6e6f0d45cd15069300202ce6cbc27e,       ->CAac6e6f0d45cd15069300202ce6cbc27e <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a> 
CAe51db5d605b94c7141d43611bc8dbbd1,       ->CAe51db5d605b94c7141d43611bc8dbbd1 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a> 
CAbe46fe9ab0202fc15184915b0af94d1a,       ->CAbe46fe9ab0202fc15184915b0af94d1a <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a> 
CA15c3eaccc8b1cfca648105744c1c1c8c,       ->CA15c3eaccc8b1cfca648105744c1c1c8c <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a> 
CAcb9a5d1f7e3f3b4f3b1eff08f4e51094,       ->CAcb9a5d1f7e3f3b4f3b1eff08f4e51094 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a> 
CAfc6a986c4e58e35778d4242303f37e32,       ->CAfc6a986c4e58e35778d4242303f37e32 <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a> 
CA58aa5dc00c72567b91b43db52577080a,       ->CA58aa5dc00c72567b91b43db52577080a <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a> 
CA65dbdee33266a706f17616ecf03e78eb,       ->CA65dbdee33266a706f17616ecf03e78eb <a href='http://api.twilio.com/2010-04-01/Accounts/My_account_nr/Recordings/RE9f96dc253140ffdfa8cd37c139de978s.json'>Audio</a> 

我正在寻找一个更好的解决方案,因为这不起作用,而且运行速度非常慢。

3 个答案:

答案 0 :(得分:1)

Twilio开发者传道者在这里。

您正在循环遍历所有呼叫,然后通过REST API循环遍历所有呼叫记录将会非常缓慢。我建议您不要采用这种方法来显示录音。

相反,你可以做两件事。

首先,编写一个类似于您已有的脚本,但不是写出HTML,而是将调用及其记录保存到数据库中。这样,您可以查看自己的数据库,这比对API进行多次调用要快得多。

其次,您可以在呼叫时使用recordingStatusCallbacks,而不是继续运行该脚本来更新新呼叫。这允许您设置webhook URL,以便在新呼叫记录完成后,您的应用程序将收到all the information on the recordings的HTTP请求。然后你也可以将它保存到你的数据库中,你的应用程序将随时更新所有最新的录音。

如果有帮助,请告诉我。

答案 1 :(得分:1)

这实际上是一种非常快速和好的方式。 解决方案由Twilio员工提供。感谢

$client = new Client($sid, $token);

// Create an array of recordings
$recording_array = array();
// Loop over the list of recordings and echo a property for each one
foreach ($client->recordings->read() as $recording) {
    $recording_array[$recording->callSid][$count] = $recording->sid;
    $count++;
}


foreach ($client->account->calls->read() as $call) {
    // Check if there is a call sid exist
    if(array_key_exists($call->sid, $recording_array)){
      foreach($recording_array["$call->sid"] as $key=>$val){
          echo $call->sid.", Recording is ".$val."\r\n";
      }
    } else {
      echo $call->sid."\r\n"; 
    }
}

答案 2 :(得分:0)