SMS and Voice Services via Twilio

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.


Find more details at Twilio Pricing.

Before you begin

    Select or create a GCP project.

    Go to the Project selector page

Setting Up

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 samples repository using git:

git clone

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.

The app.php file uses the Twilio PHP client to generate a TwiML response:

$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 HTTP POST request to http://<your app> and get the following response:

<?xml version="1.0" encoding="UTF-8"?>
  <Say>Hello Monkey!</Say>

Next, copy and paste the http://<your app> 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:

  1. Modify the index.php file and provide your "Account SID" and "Auth Token" (example) You can get these values from your Twilio account.

  2. Change the from_ and to parameters 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 to number 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);
  1. Deploy your updated code and trigger an SMS message by loading the follow request path for your app:

    http://<your app>

What's next

Now that you've learned some of the basics, learn more about additional features and some best practices for building secure and scalable applications:

¿Te ha resultado útil esta página? Enviar comentarios:

Enviar comentarios sobre...

App Engine standard environment for PHP 5