嵌入twilio录音?

时间:2016-10-15 17:51:06

标签: twilio twilio-php twilio-api

有人可以推荐将Twilio录制内容嵌入网站的最佳方法吗?

我说的是在Twilio服务器上托管的Twilio录音。

现在我只是使用我编写的下面的PHP函数来下载文件,然后找出嵌入我的方面,但可能有更简单的方法吗?

function download_recording_from_twilio($url) {

  // the file_name is everything after the twilio.com/ with an added .mp3 at the end
  $word_after_which_we_extract = "twilio.com/";

  // what is the beginning position of this word?
  $begPos = strpos($url, $word_after_which_we_extract);

  // beginning position is the beginning of the word plus the word length
  $begPos += strlen($word_after_which_we_extract);

  // everything after twilio.com/
  $everything_after = substr($url, $begPos, strlen($url));

  // position of the last / in $everything_after
  $last_slash = strrpos($url, "/");

  // everything after the last slash
  $everything_after_last_slash = substr($url, $last_slash, strlen($url));

  // full filepath
  $full_path = $everything_after.".mp3";

  // create final filename
  $file_name = $everything_after_last_slash.".mp3";

  $source = "https://api.twilio.com/".$full_path;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $source);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'ecdhe_rsa_aes_128_gcm_sha_256');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_VERBOSE,true);
  $data = curl_exec ($ch);
  $error = curl_error($ch);
  curl_close ($ch);

  $destination = ".".$file_name;
  $file = fopen($destination, "w+");
  fputs($file, $data);
  fclose($file);
}

谢谢

1 个答案:

答案 0 :(得分:1)

您可以在网页中使用<audio>元素(https://developer.mozilla.org/en/docs/Web/HTML/Element/audio)。

对于src属性,如果您想以WAV格式播放录制内容,则可以请求Twilio的录制内容api&#39;有这样的链接

https://api.twilio.com/2010-04-01/Accounts/ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/Recordings/RExxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.wav?Download=false

其中RExxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx是RECORDING SID

将.wav更改为.mp3,您将以MP3格式播放。

另外,请观察Download = false查询字符串参数。