The objective:

Build an Auto Follower app in 2 hours for Twitter. The app needs to allow users to search the Twitter for a keyword and then follow the 10 most recent tweet authors. The app also needs to limit the number of searches a user can make so that the app can’t be used for spamming 100s of twitter users.

Getting started

The first thing I need to do is decide on the scope of the app and then I can start building the code.

The app needs:

  • A Homepage that checks the current user and moves  them to either the connection page or the search page
  • A connection to the Twitter API via OAuth sign in
  • A results page showing the followers that the app has created and a link back to the homepage to start again

Twitter API

Twitter has a very thorough API which this app can hook into to do all the interaction, but in order to get the most out of the framework (and complete my task in 2 hours) I need to use a Twitter API Library.

5 minutes in…

I write in PHP so I need a library that is written in PHP and that has all the functionality I need to me able to engage with Twitter’s API. Twitter use two different types of requests when you try and access their API. The first request is a GET request – this is the easy request. A GET request usually comes in the form of a URL request. If you ever see a URL that looks something like: index.php?name=andy&user=andy, that is a GET request.

GET is easy to deal with because it doesn’t require any authentication. A simple:

File_get_contents = “url”;

will give you that particular request and then you can decode the information and do what you want with it.

The other type of request is a POST request. This type of request is used when security is an issue and when you have large amounts of information to pass through the request. I needed to find a library that would handle the POST requests and the authentication for me so that I don’t have to write 100s of lines of code.

The library I those is also one recommended by Twitter – Twitter OAuth Library.

20 minutes gone…

I’ve got the library downloaded and I’ve ran a few requests to test it out. I will need to rap the library around the pages I need to create; the homepage, the results page and the search limiter.

I’ve also thrown together a bit of a design for user friendliness.

1 hour and counting…

So heavy into the coding: Homepage – going to use a static index.html page as the page that first appears. This page doesn’t require anything other than the design and a link to the connect page from the Twitter OAuth library. The connect page then takes care of authenticating the user and redirects the user to index.php.

Index.php is that page has all my code for searching and following.

50 minutes left…

The search API uses a GET method for the request so very easy to put together. One of the parameters in a limit to the number of responses and the other parameter is the actual search term. I personally prefer to use one page to house the form HTML code and the action from that form. This just keeps things together nicely and makes it easier to edit in the future.

The code to run everything from one page requires a $_SERVER[‘PHP_SELF’] command adding to the form action. You also need an IF statement to check that the submit button has been clicked in order to start the action.

The form passes a search variable to the GET request returning the search results as an array. The reason we use an array is so that I can use the PHP function FOREACH. FOREACH cycles through each element of the array and completes a task so this is perfect for this app because I need to grab the search results and one by one find the author of the tweet and request Twitter to allow us to follow them.

The follow request is a POST request – we are basically asking Twitter to follow said person via the authenticated user that we signed in as earlier.

The FOREACH function is also used to pull out data about the user who we are just following, we need their:

  • Name
  • Profile URL
  • Profile Image

That information is then fed into the results page where you can see a picture of the user and their name and access their profile.

30 minutes and counting…

The FOREACH function is working but it is currently allowing me to follow 100s of the people. I want to limit following to 10 for each search. Again PHP has a simple way of doing this:

$i = 0;
If ($i < 10) {
//run the foreach script
//end of the foreach script
$i++

What the above does is tell the system that we variable $i starts with a value of 0 and then every time the FOREACH script is run that value is increased by 1 (using ++). The IF statement then stops the script when $i has reached 10.

The last 10 minutes…

Nearly there now… Made the connection, created the logic for adding followers and returned the results. The final bit of the job is to test the application. First I need to add a function to that when Twitter is unable to follow the user an error image is shown and the error message is displayed in the title. Also need to make sure that a user can register after their three attempts. This is handled through a $_SESSION global variable. When the user first hits the website a session is created and then 1 is added to the session every time a user makes a search. This function is a duplicate of the above – using an if statement and $_SESSION++ to increase and check the user’s session.

The end…

Finished and with 1 minute to spare – here is the latest version of my Twitter Auto Follow app. If you have any suggestions for the next 2 hour challenge leave your comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *