
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.
Getting started
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!
April 6th, 2010
Thanks for this helpfull post. I would be very interested in the code you used!
« Reply
April 8th, 2010
This looks great, I’m just about to launch a Twitter comp and this has been very helpful.
« Reply
July 25th, 2010
Hey nice post but how to enter all my followers data into my database? Can you help me out on that please!
Thanks in Advance!
« Reply
November 9th, 2010
Hi There,
I’m about to run a competition in twitter but slightly confused on how or where to setup the sql. Also what is a CRON job?
Thanks for your help, Frano
« Reply
June 7th, 2011
I am setting up a system just like this, or attempting to, and am currently trying to piece together a way using either WordPress and widgets, or Joomla and modules, and then delivering sorted search results to boxes, rather complicated. I’d be extremely interested in your source code for this and willing to give you a featured twitter feed on our website as thanks for the help.
We are currently working on “Music Tweet Off’s” where two bands have their fans, or our fans check out the bands, and vote using a hashtag and the bands twitter name, and to score an extra vote and be entered into a random prize drawing they can goto our website and vote via a on site voting poll.
If we used MySql and PHP we could integrate the website and twitter votes as well as votes on Facebook if we went that direction and made voting available on the fan page.
« Reply