How To Create a Simple SMS Voting System Using PHP

 You can find an updated, 5 part tutorial showing you how to build a Twilio Powered SMS Voting System here

SMS voting systems are incredibly useful at larger events where you want to poll the attendees, but don’t want to limit yourself to only those with smartphones. Using Twilio’s SMS API, you can create a simple voting system in just a few minutes.

Prerequisites

Before you get started, you’ll need a Twilio account and a web server running PHP 5 or higher. You can sign up for a free Twilio account and get $30 in credit here. There are many web hosting services that support PHP, so I recommend looking around and finding one that fits you well.

Storing the Votes

Our votes have to go somewhere, so we’ll start with storage. Since we’re keeping it simple, we’ll use SQLite, and wrap all our database functions in a thin database class.
As you can see from the init function, we have two tables in our database, one to store the teams being voted on (teams) and one to store the people that have voted (votes). We store the voters so that we can prevent people from voting more than once. We also have functions to add a team to the database, as well as code to save a vote and get the information about a team.

We’ll also create a small setup script that’ll create our database and add the teams we’ll be voting on. Feel free to change the team names or add more teams. (Since this is a setup script, you can delete it after you run it.)

Gathering the Votes

When a text message is sent to your Twilio phone number, Twilio takes all the data and sends it to your web app as a web request. So handling votes is as easy as taking that data and saving it to the database.

Configuring Twilio

Go to your Numbers page and click the blue “Buy A Number” button to purchase a number. This will be the number people will text in to when voting. (You’ll need to upgrade your Twilio account in order to purchase a number.)

Tell Twilio where to send incoming SMS messages by going to your Numbers page, clicking on your number, and setting the SMS URL to the handle-incoming-sms.php script.

Displaying the Votes

Once the votes have been cast, displaying the result is just a matter of querying the database and showing the vote count.

Wrapping up

The code present is pretty basic, but is a good example of just how little is needed to create an SMS voting system using PHP. A slightly prettier, though not by much, version that displays the votes in realtime can be found here.

  • seems like lot of work..I just ran a text voting campaign using txt2vote service from this company
    http://www.txtimpact.com, it was pretty simple, all done viw web interface, no need to write any program and also.. they have short code instead of long numbers…

    Keep texting!!

    • Anonymous

      True, but it’s fun to make something yourself once in a while. Plus, it’s a great way to play around with/learn the Twilio API, which you can then use to make more powerful things like IVRs or even your own text to vote company. (One of our customers, PrecisionPolling, built such a system and sold it to Survey Monkey.)

      As for work, to be fair, most of the code is involved with the routine stuff you’d need to do even if it weren’t voting by SMS (creating a database, inserting data, displaying data). The portion to SMS enable it is just handle-incoming-sms.php.

    • Solarfret

      Txtimpact is twice as expensive …

  • seems like lot of work..I just ran a text voting campaign using txt2vote service from this company
    http://www.txtimpact.com, it was pretty simple, all done viw web interface, no need to write any program and also.. they have short code instead of long numbers…

    Keep texting!!

    • Anonymous

      True, but it’s fun to make something yourself once in a while. Plus, it’s a great way to play around with/learn the Twilio API, which you can then use to make more powerful things like IVRs or even your own text to vote company. (One of our customers, PrecisionPolling, built such a system and sold it to Survey Monkey.)

      As for work, to be fair, most of the code is involved with the routine stuff you’d need to do even if it weren’t voting by SMS (creating a database, inserting data, displaying data). The portion to SMS enable it is just handle-incoming-sms.php.

  • Rahim! Great guide… just what I needed to push me to dig in a little deeper.

    Minor edit on this guide and readme: url for the voice should be the xml file (Text-to-Vote/handle_incoming_call.xml) & method set to GET in Tw dashboard

    The voice test worked fine for me, but two things on the SMS…

    my test is here: http://little.am/Text-to-Vote

    I cloned from github ‘Text-to-Vote’ but keep getting errors in my Tw dashboard (Document parse failure – ErrorCode: 12100)… there’s no body to my XML that Tw gets, but in the browser it seems fine: http://little.am/Text-to-Vote/handle_incoming_sms.php

    I’m also getting invalid argument errors in the foreach ($teams as $team) loop: http://little.am/Text-to-Vote/get_votes.php

    Trying to recreate this in ruby/rails – just started playing with it – so we’ll see.

    Thanks!!
    –bp

    • Anonymous

      Hi Brandon,

      Thanks for the catch! I’ve updated the readme on github to handle-incoming-call.xml. As for the HTTP method, either GET or POST will work for the Voice URL.

      Strange that the database got created but not the tables. Did it sort itself out after a few rounds of deleting and reruning?

  • Rahim! Great guide… just what I needed to push me to dig in a little deeper.

    Minor edit on this guide and readme: url for the voice should be the xml file (Text-to-Vote/handle_incoming_call.xml) & method set to GET in Tw dashboard

    The voice test worked fine for me, but two things on the SMS…

    my test is here: http://little.am/Text-to-Vote

    I cloned from github ‘Text-to-Vote’ but keep getting errors in my Tw dashboard (Document parse failure – ErrorCode: 12100)… there’s no body to my XML that Tw gets, but in the browser it seems fine: http://little.am/Text-to-Vote/handle_incoming_sms.php

    I’m also getting invalid argument errors in the foreach ($teams as $team) loop: http://little.am/Text-to-Vote/get_votes.php

    Trying to recreate this in ruby/rails – just started playing with it – so we’ll see.

    Thanks!!
    –bp

    • Anonymous

      Hi Brandon,

      Thanks for the catch! I’ve updated the readme on github to handle-incoming-call.xml. As for the HTTP method, either GET or POST will work for the Voice URL.

      Strange that the database got created but not the tables. Did it sort itself out after a few rounds of deleting and reruning?

  • Deepu

    can you tell me how to write a code for sending sms to mobile through my personel website using php code…. (without registering any sms api gateway software) …..

    Thanks in advance

  • Deepu

    can you tell me how to write a code for sending sms to mobile through my personel website using php code…. (without registering any sms api gateway software) …..

    Thanks in advance

  • sweety

    me to want same code for sending sms to mobile ….. please tell me if  anyone know the process

  • sweety

    me to want same code for sending sms to mobile ….. please tell me if  anyone know the process

  • Curios

    doesent have a valid she bang line??

  • DarrinStern

    Can someone help me, if I want to left people vote as much as they want…where would I make that change? Also where can I change the response sms people get?

  • aakash

    Hello Sir,
    I own the Result website : http://www.whatismyresults.com and wants to implement this idea.
    Can I do so ?

    • Hey Aakash,

      We’d love to learn more about what type of implementation you have planned. We’re here to help if you have any questions.

  • Gaurav Chopra

    nice Technique for voting as a Web system . thanks for suggesting me . Please tell me can i use thin on wp blog – http://timeeducation.in/

    • Hey Gaurav,
      Were you asking if you can use the post on your blog? If you have any questions about syndicating content, please shoot an email to kyleky@twilio.com

      • Gaurav Kumar

        Thanks Twilio ,,,, but now my problem is resolve but i faced same problem on another blog (http://www.recruitmentguides.com ) . So need your help . again Thanks for last one

  • Narender

    can you tell me how to write a code for sending sms to mobile through my
    personel website using php code…. (without registering any sms api
    gateway software) ..
    http://www.freshzene.com

  • GMEHTA89

    Hi Brandon Dear ,

    Thanks to the catch! I’ve updated the readme in github to handle-incoming-call. xml. As for the HTTP method, either GET or POST is wonderful for the Voice LINK.http://www.sscresultportal.com/

    Strange that the particular database got created however, not the tables. Achieved it sort itself out from few rounds of deleting and reruning?

  • Krishna Bogati

    Trekking in Nepal

    http://www.gotonepal.com

  • Vikas

    I am currently working on a news website http://www.resultswave.com/ is it possible to implement this voting system on my website? Please reply back to my email if it is possible.

  • Sechmail

    Hello, I wanted to find out, who pays for the text the voter or the twilio client

    • Hey@ Sechmail, standard messaging rates apply for the person sending the text message to the Twilio powered number. The owner of the voting system also pays 1 cent per text message received. You can also find a more up to date post on building Twilio powered voting systems here: https://www.twilio.com/blog/tag/votr

  • Thankss for Creating SMS Voting System for my New Site. Great Result