Twilio Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

Building an emergency calling system

I'm a type-2, insulin-dependent diabetic. I also drive two hours every day—to my office and back. So, after I spent a week in the hospital last year, I decided to set up an In Case Of Emergency (ICE) system so that I could call one number and have it try multiple numbers at once.

Getting ready

The complete source code for this recipe can be found in the Chapter2/Recipe4 folder.

How to do it...

This emergency calling system will try a group of numbers at the same time; the first number to answer will get connected.

  1. Download the Twilio Helper Library from https://github.com/twilio/twilio-php/zipball/master and unzip it.
  2. Upload the Services/ folder to your website.
  3. Upload config.php to your website and make sure the following variables are set:
    <?php
      $accountsid = '';  //  YOUR TWILIO ACCOUNT SID
      $authtoken = '';  //    YOUR TWILIO AUTH TOKEN
      $fromNumber = '';  //  PHONE NUMBER CALLS WILL COME FROM
    ?>
  4. Create a file on your website called ice.php, with the following code:
    <?php
    session_start();
    include 'Services/Twilio.php';
    include("config.php");
    $client = new Services_Twilio($accountsid, $authtoken);
    $response = new Services_Twilio_Twiml();
    $timeout = 20;
    $phonenumbers = array(
      '1234567890',
      '1234567891'
    );
    $dial = $response->dial(NULL, array('callerId' => $fromNumber));
    foreach($phonenumbers as $number){
      $dial->number( $number );
    }
    header ("Content-Type:text/xml"); 
    print $response;
    ?>
  5. To have a number point to this script, upload ice.php somewhere and then point your Twilio phone number to it.
    How to do it...

    Insert the URL to this page in the Voice Request URL box. Then, any calls that you receive at this number will be processed via ice.php.

How it works...

In steps 1 and 2, we downloaded and installed the Twilio Helper Library for PHP. This library is at the heart of your Twilio-powered apps.

In step 3, we uploaded config.php that contains our authentication information to communicate with Twilio's API.

In step 4, we uploaded ice.php.

Finally, in step 5, we configured a phone number to direct all calls to ice.php.

We then set up the $phonenumbers array with the phone numbers in this recipe, which you should try.

When a user calls the phone number you assigned to ice.php, it tries all the numbers in the $phonenumber array at once and connects the call to the first one that answers.

You can store this number at a prominent location on your contacts list so that, in case of emergencies, you can quickly dial the number. I usually save the number to a contact named ICE and add it to favorites so that it is at the top of the list.