PHP Quickstart: Greeting Caller By Name

Greet Caller by Name

Ok, that was fun. Seriously, it was. But let's raise the stakes a bit. When someone calls, we are going to try to greet the caller by name:


    // 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>

In this example, the code inspects the From parameter passed by Twilio in the request to get the Caller ID of the caller. We then check to see if the caller is known looking up the number in the $people array to extract their name. We then Say their name or the word 'Monkey' if their Caller ID isn't known.

If you add your phone number and name to the $people array you can listen to Twilio greet you by name. Note that the phone numbers in the $people array have +1 prepended to them. '1' is the international country code for the US and Canada and the '+' formats the number in E.164 format which is used by Twilio when passing you phone numbers.

Next: Play mp3 for Caller »

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.