You can use Twilio to embed voice, VoIP, and messaging into applications. The Twilio platform consists of the Twilio Markup Language (TwiML), a RESTful API, and VoIP SDKs for web browsers, Android, and iOS. Helper libraries are available in multiple languages.
TwiML tells Twilio what to do when you receive an incoming call or SMS. When someone makes a call or sends an SMS to one of your Twilio numbers, Twilio looks up the URL associated with that phone number, makes a request, and then reads TwiML instructions at that URL to determine what to do, such as record a call, or send an SMS.
You can use the Twilio REST API to query metadata about your account, phone numbers, calls, text messages, and recordings. You can also initiate outbound calls and send text messages.
Before you begin
Select or create a Cloud Platform Console project.
You will use the standard Google App Engine PHP Runtime Environment to construct this example. If this is your first time writing PHP for Google App Engine, you might first try the Guestbook tutorial for PHP.
First, clone the
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
Optionally, download the zip and extract it.
Next, change to the application directory and install the dependencies via composer:
cd php-docs-samples/appengine/standard/twilio composer install
Receiving an Incoming Call
To create your first application with Twilio:
Let's walk through creating your first application, Hello Monkey. You will use the twilio-php library.
Follow these directions to deploy your project to App Engine: Uploading your Application.
app.php file uses the Twilio PHP client to generate a
$response = new Services_Twilio_Twiml(); $response->say('Hello Monkey'); return (string) $response;
After you've deployed your project to App Engine, you can send a
POST request to
http://<your app>.appspot.com/twiml and get the
<?xml version="1.0" encoding="UTF-8"?> <Response> <Say>Hello Monkey!</Say> </Response>
Next, copy and paste the
http://<your app>.appspot.com/twiml URL into the
"Voice" URL box on the Numbers page of your Twilio Account.
Now call your Twilio number! You should hear a voice say "Hello Monkey!" in response. When you call, Twilio will fetch your URL, and execute the XML instructions listed above. Then, Twilio will hang up, because there are no more instructions.
Sending an SMS
To send a SMS message using Twilio:
toparameters to use real phone numbers. The
from_number must be a valid Twilio phone number in your account. For this example, use the phone number you called in the example above. The
tonumber can be any outgoing number, such as your cell phone number.
The app will send a text message using your supplied credentials:
$client = new Services_Twilio($sid, $token); $sms = $client->account->messages->sendMessage( $fromNumber, // From this number $toNumber, // Send to this number 'Hello monkey!!' ); return sprintf('Message ID: %s, Message Body: %s', $sms->sid, $sms->body);
Deploy your updated code and trigger an SMS message by loading the follow request path for your app:
Now that you've learned some of the basics, learn more about additional features and some best practices for building secure and scalable applications: