PHP Quickstart: Sending Messages during a phone call

We've responded to incoming SMS messages directly, but what about sending SMS during phone calls? Now we will send a message to anyone who calls our Twilio phone number. If you haven't yet, read over the TwiML Quickstart, as this example utilizes concepts introduced in that guide.

Next, set the voice URL of your Twilio phone number to where is the name of your server and you have placed the sms-twiml.php file on your server.


    // 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>
    <Sms><?php echo $name ?>, thanks for the call!</Sms>

When someone calls your Twilio phone number, the caller will be greeted by name and then sent an SMS message thanking them for calling. Isn't that nice?

For more information, see the <Sms> verb documentation.

And that's all she wrote folks! Back to Quickstarts.

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.