In this tutorial I’ll talk you through the process of hosting a twitter competition using PHP and MySQL in conjunction with the twitter API, as well as giving you some points to consider. This all started a while back when I decided to dabble in social media marketing using twitter. The idea was simple; win a prize by following me(@philipbeel) and tweeting or re-tweeting #theodin within a set time period. This worked extremely well and I gained around 200 followers, so I thought I would share the process with you.
In order to run a twitter competition you will need a couple of things. Firstly, and most importantly make sure you have set up a twitter account.
You will also need a term or hash tag ‘#’ that is not already in use. This can be anything you like, but remember tweets are limited to 140 characters, so the shorter the better.
What are the rules
In order to run a competition fairly you will need to outline your rules clearly and adhere to them. Although you can specify any rules you like, here is a general guide:
- the competition will be run for a defined period of time.
- entrants must follow a nominated twitter account.
- entrants may only be counted once.
Issues for consideration
A common issue with using any API is that you can never rely on it 100%. Twitter is no exception. Unfortunately at the time of writing twitter does not enable you to publicly search for tweets beyond a certain date. They have not been deleted, they are simply not available at present. This may change in future, but as we are dealing with the present a simple solution is to use a MySQL database to store your entrants in.
Storing your entrants
You will need a way to store our entrants. for the sake of simplicity I have layed out a basic table structure in MySQL, in which we can store an ID and the twitter username. The table should look something like this:
CREATE TABLE IF NOT EXISTS `twitter_comp` ( `ID` int(115) NOT NULL auto_increment, `username` text NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
In order to get the entrants into the database you will need to use PHP and the twitter API. The process is outlined below:
- Use the twitter API to collect all your followers and store them in an array.
- Search the twitter API for all the # tags which you have specified, then store the @username into another array.
- Use the array_unique() function on the entrants array to ensure that no matter how many times a user tweeted your # tag, they are only stored once.
- Then do a compare on the entrants array to keep only @usernames of your confirmed followers. Ideally create a new array from this.
- At this point you can then store the entrants in the database.
Updating your entrants
Over time, as more entrants tweet the # tag the the steps above should be repeated. Ideally this should be wrapped into a function which runs when the page is triggered, automating this with a daily CRON job will make life a lot easier.
Drawing a winner
Once the competition has drawn to a close, you will have a database full of legitimate entrants to the competition. Now the fun part drawing a winner, you can do this following the steps below:
- Connect to the database and pull out all of the @usernames.
- Store the @usernames in an array.
- Ensure no duplication has taken place by running another array_unique on the entrants.
- Mix it up a bit using PHPs shuffle() and array_rand() functions.
- Finally draw out the winner and present it on screen.
Announcing the winner
Once you have closed the competition and drawn the winner you should first DM the @username privately before telling the world. This ensures that you have a real person and you can go further by exchanging contact details and arranging delivery of the prize. Then you are free to tell the world who won and publicize the event.
I hope this post is helpful to anyone looking to start their own competition. If this post gets a lot of interest I would be happy to share the code which I wrote when I ran my own competition. Thoughts, considerations and comments are all welcome. Good luck!