PHP Quickstart: Recording Caller's Voices

Easy so far... how about letting the caller record her own monkey howl? Sounds like fun.


    // make an associative array of callers we know, indexed by phone number
    $people = array(
        "+14158675309"=>"Curious George",

    // if the caller is known, then greet them by name
    // otherwise, consider them just another monkey
    if(!$name = $people[$_REQUEST['From']])
        $name = "Monkey";

    // now greet the caller
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    <Say>Hello <?php echo $name ?>.</Say>
    <Gather numDigits="1" action="hello-monkey-handle-key.php" method="POST">
            To speak with a real monkey, press 1.  
            Press 2 to record your own monkey howl.  
            Press any other key to start over.

We added an extra option in the <Say> block telling the caller if she presses '2' she can record audio. The next step is to add an additional processing instruction to our <Gather> 'action' handler.


    // if the caller pressed anything but 1 or 2 send them back
    if($_REQUEST['Digits'] != '1' and $_REQUEST['Digits'] != '2') {
        header("Location: hello-monkey.php");

    // otherwise, if 1 was pressed we Dial 3105551212. If 2 
    // we make an audio recording up to 30 seconds long.
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
<?php if ($_REQUEST['Digits'] == '1') { ?>
    <Say>The call failed or the remote party hung up.  Goodbye.</Say>
<?php } elseif ($_REQUEST['Digits'] == '2') { ?>
    <Say>Record your monkey howl after the tone.</Say>
    <Record maxLength="30" action="hello-monkey-handle-recording.php" />
<?php } ?>

We added a new option to handle the case when someone presses '2'. We can again simulate the pressing of the '2' key by adding ?Digits=2 to the URL.

The handler now outputs a new response asking for your hoooooooooowl. The <Record> tag lets you record audio from the caller.

  • The 'maxLength' parameter tells Twilio to record up to 30 seconds of audio.
  • The 'action' parameter tells Twilio what to do after the caller is done recording. In this case we pass off to the hello-monkey-handle-recording.php page.

    // tell the caller that they should listen to their howl
    // and play the recording back, using the URL that Twilio posted
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    <Say>Thanks for howling... take a listen to what you howled.</Say>
    <Play><?php echo $_REQUEST['RecordingUrl']; ?></Play>

The recording handler simply plays the recording back for the caller and says goodbye. The URL of the recording is passed to the handler in the request as the RecordingUrl parameter. The URL is externally accessible so you can send the URL of the howl to your friends or reference it in a webpage. Check out the Voice Recorder demo for another example of making voice recordings.

Next: Make outgoing calls »

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.