Phone Poll

Phone polls are a simple way to gather lots of information in a short manner. Using Twilio you can easily construct a phone polling system. In this article I will go over how to build a simple a phone poll that will allow you to call your friends and ask them what kind of pizza topping they would like for an upcoming party.


A PHP script goes through a list of friends and calls each of them, and then asks them what kind of pizza topping they would like, they use the keypad to enter in their selection.


This demo demonstrates initializing a call using the Twilio REST API and creating TwiML call logic with focus on the <Gather> and <Say> verbs.



  • 1

    • First we create an array that will hold the phone numbers of your friends.

        Next a FOR loop goes through and takes the phone numbers from the array and sends them (via POST) to poll.php.

          • howtos/phonepoll/makecall.php
            // Instantiate a client to Twilio's REST API
            $client = new Services_Twilio($sid, $token);
            foreach ($numbers as $number) {
            	try {
            		$call = $client->account->calls->create(
            			'415-555-7777',									// Caller ID
            			$number,												// Your friend's number
            			''	 // Location of your TwiML
            		echo "Started call: $call->sid\n";
            	} catch (Exception $e) {
            		echo 'Error starting phone call: ' . $e->getMessage() . "\n";
      • 2

        • The polling system relies on a series of nested <Say> verbs which list off the pizza toppings to your friends. This is all inside of the <Gather> verb, which allows your friends to enter in their selection using the keypad.

            • howtos/phonepoll/poll.php
              require 'Services/Twilio.php';
              $response = new Services_Twilio_Twiml();
              $gather = $response->gather(array(
              	'action' => '',
              	'method' => 'GET',
              	'numDigits' => '1'
              $gather->say("This is Mark's Pizza Party Poll");
              $gather->say("If You Would Like Cheese Pizza Press 1");
              $gather->say("If You Would Like Pepperoni Pizza Press 2");
              $gather->say("If You Would Like Sausage Pizza Press 3");
              $gather->say("If You Would Like Pineapple Pizza and Canadian Bacon Press 4");
              header('Content-Type: text/xml');
              print $response;
        • 3

          • When your friends make their selection the results are sent back to the page (via GET).
        • 4

          • The choice the user made is sent to the processing page in the form of a GET Variable called Digits. This can be accessed by $_REQUEST['Digits']. If the digits variable is set the corresponding selection is added to the database.

              • howtos/phonepoll/process_poll.php
                	// Check if values have been entered
                	$digit = isset($_REQUEST['Digits']) ? $_REQUEST['Digits'] : null;
                	$choices = array(
                		'1' => 'Cheese',
                		'2' => 'Pepperoni',
                		'3' => 'Sausage',
                		'4' => 'Pineapple_Bacon',
                	if (isset($choices[$digit])) {
                		mysql_query("INSERT INTO `results` (`" . $choices[$digit] . "`) VALUES ('1')");
                		$say = 'Thank you. Your choice has been tallied.';
                	} else {
                		$say = "Sorry, I don't have that topping.";
          • 5

            • After the selection is added the TwiML outputs a thank you message, and the phone call is ended.

                • howtos/phonepoll/process_poll.php
                      require 'Services/Twilio.php';
                  	$response = new Services_Twilio_Twiml();
                  	header('Content-Type: text/xml');
                  	print $response;