A Few Notes about Twilio TwiML Transcriptions

Twilio Logo

Recently, we had to create an Interactive Voice Response (IVR) application in PHP using the Twilio TwiML API. Part of this task involved retrieving transcriptions of the called party responses using the TwiML API, so they could be stored in the database.  While doing the work, we noticed some aspects that are not mentioned in the TwiML documentation:

Transcription callbacks do not contain session info.

If you set a session cookie for the TwiML calls, the responses will include said cookie, but not the transcription callbacks. To work around this, you can add GET parameters to the callback URL in the TwiML call.

Transcription callback URLs.

You can use full (https://example.com/twilio_app/callback_handler.php), or relative (twilio_app/callback_handler.php or callback_handler.php) URLs.

Empty voice responses do not generate a transcription.

When the called party leaves an empty voice response, the transcription callback is not generated. While developing the application we expected to receive an empty transcription, and we spent some time wondering what could be wrong in the code until Twilio Support told us about this behavior.

It seems other developers have also found this confusing, so it’s possible that Twilio will implement callbacks on empty transcriptions in the future.

If you need help with your Twilio application, you can contact us.

Twilio Tutorial: Sending an SMS with the PHP REST API

Note: this post was previewed and approved by the customer.

Twilio Logo
Twilio Logo

Update: One day after publishing this post, Kevin from Twilio suggests in the comments to use the last version from Github instead the 3.2.3 twilio-php library zip archive from the Twilio page. That fixed an SSL certificate validation issue. This is what we call great support!

In a recent project, we used the Twilio REST API to send SMS messages. Here is a very simple tutorial about it.

If you need help integrating Twilio in your application, you can contact us.

1. Register your phone number with Twilio (or get  a production account).

At the time of writing this post, Twilio offers a trial account with $30 free credit. Just go to the Twilio site and create a new one. The trial account requires verification of the phone numbers you want to use with their service. You can do this, at no extra cost, by going to Numbers → Verify a number in their control panel. You will then receive a call to that number asking for confirmation.

For your caller ID, you can use the sandbox number you will find after logging in to, buy a number from them or use a verified number. Alternatively, you can upgrade your account to a production one, which will allow you to use any phone number (you have to register a credit card with them, but you don’t need to make a payment until you spend the initial $30 credit).

2. The configuration file

Now, download the twilio-php library from their site (I used version 3.2.3 for this tutorial), or get it from Github  and install it in your server (I used the twilio-lib directory). In the root of your site (or in your test dir) create a twilio.config.php file with the following:


$AccountSid = "Your Account SID";
$AuthToken = "Your Auth Token";

Replace the values with the ones you will find after login into Twilio.

3. Send an SMS.

This is pretty straightforward:


try {
    // Instantiate a new Twilio Rest Client
    $client = new Services_Twilio($AccountSid, $AuthToken);

     * If you get an "Services_Twilio_TinyHttpException: SSL certificate problem [...]";
     * exception, uncomment the two lines below. More about this issue at
     * http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
     * UPDATE: using a twilio-php library version newer than 3.2.3 or the last version
     * from Github fixes this issue, making the two lines below unnecessary.

     // $http = new Services_Twilio_TinyHttp("https://api.twilio.com", array("curlopts" => array(CURLOPT_SSL_VERIFYPEER => false)));
     // $client = new Services_Twilio($AccountSid, $AuthToken, "2010-04-01", $http);

     * The number we are sending from, it must be a valid Twilio number
     * You can use the default number which you will find in the sandbox,
     * buy a number from them or port your own number to Twilio.
    $from = "415-599-2671"; // this is the default sandbox number
    // The number we are sending to - any phone number
    $to = "A Phone Number";
    // The SMS body
    $message = "Testing the Twilio SMS REST API";

    // Send a new outgoing SMS
    $response = $client->account->sms_messages->create($from, $to, $message);

    // Get the delivery status
    echo $response->status;
} catch (Exception $e) {
    echo "Caught exception: " . $e->getMessage();

4. Check the results.

If everything went well, you will receive an SMS message in your phone. You can also go to Logs → SMS in the Twilio site and see the results there:

Twilio SMS Logs
Twilio SMS Logs Screen

Any questions? Feel free to post a comment.