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][tq], as this example utilizes concepts introduced in that guide.

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

Loading Code Samples...
Language
Format:
  • TwiML
<?php
// Get the Twilio-PHP helper library from twilio.com/docs/libraries/php,
// following the instructions to install it with Composer.
require_once "vendor/autoload.php";
use Twilio\Twiml;

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

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

$response = new Twiml();
$response->say("Hello {$name}");
$response->message("{$name}, thanks for the call!");

header("content-type: text/xml");
echo $response;
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say>
        Hello Curious George.
    </Say>
    <Message>
        Curious George, thanks for the call!
    </Message>
</Response>
Send SMS during Phone call using Twilio

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?

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.

1 / 1
Loading Code Samples...
Format:
  • TwiML
<?php
// Get the Twilio-PHP helper library from twilio.com/docs/libraries/php,
// following the instructions to install it with Composer.
require_once "vendor/autoload.php";
use Twilio\Twiml;

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

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

$response = new Twiml();
$response->say("Hello {$name}");
$response->message("{$name}, thanks for the call!");

header("content-type: text/xml");
echo $response;
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say>
        Hello Curious George.
    </Say>
    <Message>
        Curious George, thanks for the call!
    </Message>
</Response>