Processing recordings with PHP
June 10th, 2010 by Adam KalseyWhen you tell Tropo to record audio, either through a call recording or by using the record command to record a specific prompt, Tropo then sends that audio to your server. You can give Tropo FTP credentials and we’ll FTP it over to you, or you can give an http URL and we’ll make an POST to that URL containing your file.
When Tropo POSTS an audio file, we submit it as multipart form data, so the same tools you’d use to process a file upload field on a form can be used to save your recording. The form field name Tropo uses for the file is “filename“.
Here’s an example using the Tropo WebAPI, PHP, and the PHP WebAPI library. In this example, I’m building a simple voicemail recorder, asking the caller to leave a message at the beep, and then saving the recorded audio to my server.
I’m also introducing some logging, using the excellent KLogger class to write a log file every time we save a recording.
First, a walkthrough of the important parts of the code, then a link to the full code at the end.
<?php
$tropo->record(array(
'say' => 'Leave your message at the beep.',
'url' => getself() . '?record', // append ?record to the URL
));
function getself() {
$pageURL = 'http';
$url = ($_SERVER["HTTPS"] == "on") ? 'https' : 'http';
$url .= "://" . $_SERVER["SERVER_NAME"];
$url .= ($_SERVER["SERVER_PORT"] != "80") ? ':'. $_SERVER["SERVER_PORT"] : '';
$url .= $_SERVER["REQUEST_URI"];
return $url;
}
?>
Create a recording prompt, saying “Leave your message at the beep.” Tropo has a beep parameter that you can use to turn off the beep if you want. It’s on by default, and we want to leave it on here. We also add the URL where we’d like to send the recording using a simple function that gives us the full URL of the current application. Appended to the URL is ?record, a flag we’ll use to see if the request to our app is an incoming call or a call recording being posted.
This PHP code generates the follow JSON that’s sent to Tropo:
{
"tropo": [
{
"record": {
"say": {
"value": "Leave your message at the beep."
},
"url": "http://example.com/recordingdemo.php?record"
}
}
]
}
So now to save the file…
<?php
$target_path = 'path/to/recording/' . $_FILES['filename']['name'];
if(move_uploaded_file($_FILES['filename']['tmp_name'], $target_path)) {
$log->LogInfo("$target_path [{$_FILES['filename']['size']} bytes] was saved");
} else {
$log->LogError("$target_path could not be saved.");
}
?>
In this code, which we only run if the ?record flag is present, we use PHP’s global $_FILES array to fetch the file data out of the multipart form post. We start by setting the path and filename we’d like to use for saving the file (line 2) using the filename that Tropo gives us in $_FILES['filename']['name']. Then in line 3, we move the uploaded file from the temporary location that PHP stores a new upload in to the path we chose. If the file is moved successfully, we write a log entry with the path and file size (line 4). If an error occurred, we note that in the log on line 6.
To get the whole sample code, grab the Tropo WebAPI PHP library from github and look in the Samples directory or just grab the sample off Github directly.
Related posts:
