我目前正在努力解决一个问题,即少数录音是空的(从 Twilio 的服务器下载它们时),而它们不应该是空的。
我的应用程序目前每天负责大约 10,000 个呼叫。这些呼叫中的每一个,如果得到应答,都会被记录下来,然后该记录会下载到我的服务器上。
无论出于何种原因,这些下载的 mp3 文件中有少数是空的,它们的大小为 363 字节(没有音频,只有文件本身),即使我知道 mp3 文件中应该有内容。例如,某些通话将持续 30-40 分钟,但下载时录音为空。
我正在使用 PHP 进行开发,并附上了用于下载下面记录的代码。有什么看起来偶尔会引起问题的吗?我已经尝试搜索其他人可能遇到的类似问题,到目前为止还没有发现任何东西。
请记住,此代码适用于 90% 的情况……只是那些无法正确下载的 10%。
非常感谢您的时间!
public function index()
{
if (isset($_REQUEST['RecordingUrl'])) {
// Get the call ID from the url
$confId = $this->security->xss_clean($_GET['confId']);
$callId = $this->security->xss_clean($_GET['callId']);
$userId = $this->security->xss_clean($_GET['userId']);
// Twilio account information
$accountSid = $this->config->item('twilio_accountSid');
$authToken = $this->config->item('twilio_authToken');
// Download the recording to our server
$callUrl = 'recordings/calls/' . $callId . '.mp3';
$this->getMp3($_REQUEST['RecordingUrl'], $callUrl);
// Delete the recording from Twilio's servers
$this->deleteMp3($_REQUEST['RecordingUrl'], $accountSid, $authToken);
// Mark this call as completed and update its log
$this->call->logEndOfCall($callId);
// Load a blank Twilio response to keep the server from throwing an error
$this->load->view('twilio/blank_twiml');
}
}
private function getMp3($sourceUrl = '', $destinationUrl = '')
{
// Add the '.mp3' onto the end of the url to return an mp3 file
$sourceUrl = $sourceUrl . '.mp3';
// Set the variables and initialize cURL
$destinationUrl = '/' . $destinationUrl;
$fp = fopen($destinationUrl, 'w+');
$ch = curl_init();
$timeout = 300;
// Sleep for two seconds to prevent a race condition with Twilio
sleep(2);
// Set the options for cURL to download the file
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $sourceUrl);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// Execute the cURL
curl_exec($ch);
// Save any information for future debugging purposes
$info = curl_getinfo($ch);
// Close the new mp3 file and cURL
curl_close($ch);
fclose($fp);
}
private function deleteMp3($url = '', $username, $password)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
$result = curl_exec($ch);
curl_close($ch);
}