How To Create a Simple SMS Voting System Using PHP
Time to read: 2 minutes
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.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.