PHP Quickstart: Connecting Call to Second Person

Connect Call to a Second Person

Hoot. Now, let's also ask the caller if she wants to talk to a real monkey. If she does, connect her to Koko, the famous Gorilla, whose phone number we happen to know is 310-555-1212 :)


    // 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">
        <Say>To speak to a real monkey, press 1.  Press any other key to start over.</Say>
  • On line 24 we ask the caller to press a key to talk to a monkey or any other key to replay the menu.
  • Notice how the <Say> block is nested inside a <Gather> block which starts on line 23.
  • When Twilio sees a <Gather> block it starts looking for input from the caller's key pad. Because the <Say> is nested inside the <Gather>, Twilio will detect if caller presses a key even if the key is pressed while the <Say> is still running.
  • When the caller presses a key, the pressed key is POSTed to the URL specified by the 'action' handler attribute of the <Gather> verb. In this case, the URL is hello-monkey-handle-key.php.

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

    // the user pressed 1, connect the call to 310-555-1212 
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    <Say>The call failed or the remote party hung up. Goodbye.</Say>

The keys pressed by the caller are transferred to the action handler as the value of the Digits parameter. In this case, the POST HTTP method is used with the <Gather> block so the value is sent as as url-encoded POST parameter. PHP has a nice feature, the '$_REQUEST' array, that lets you grab the values of request parameters regardless of the request method type. This allows us to simulate the Twilio Digits request and test our response handler using HTTP GET, which is easier to use. For example, putting the following URL into your web browser simulates the caller pressing the digit '1':

The purpose of the 'action' handler is to forward the caller back to the main menu using the PHP header("Location: hello-monkey.php"); method if the caller did not press '1', and <Dial> a number if the caller pressed '1'.

  • The <Dial> verb lets you connect one call to another. If you want to initiate a new outgoing call rather than connect an existing call to another number check out the Twilio REST API Calls resource.
  • Notice how there is a <Say> block below the <Dial> verb. The <Dial> will 'fall-through' if the call fails, the person never picks up, or the line is busy. In these cases the <Say> block underneath will execute and wish the caller goodbye!
  • The <Dial> verb will also fall-through to the <Say> if the remote caller hangs up the phone. This lets you place additional logic after the call like using <Dial> to connect to another number.

Next: Record a message »

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.