Fetching Tweets

Using PHP and Twitter API

Posted on October 15, 2015 in webdev

Sometimes job interviews for a developer position can be daunting. Images of a blank whiteboards and blank stares may come to mind. It’s always nice when you can choose a mini-project over a whiteboard interrogation. Just a few weeks ago I was challenged to complete two mini “aptitude tests” as a preliminary qualification for an in-person interview.

Challenge

Here is one of the challenges that I was given:

Use php or javascript to connect to and pull down content (e.g., tweets, pins, posts, statuses) from any one of the following social networks: LinkedIn, Twitter, Instagram, Pinterest, or Facebook.

I was happy to see Twitter in the list because I had previously worked with Twitter’s API to automatically tweet a word of the day. The only difference with this project was that instead of posting tweets, I would be retrieving tweets, and instead of using python, I would be using PHP or Javascript.

Approach

Twitter is known to have a very user-friendly API and the documentation’s design reflects its friendliness. There are many tools that can help you integrate. One of them is TwitterOAuth, made especially for PHP.

I installed TwitterOAuth, followed the documentation to connect to Twitter’s API, used $twitter->get to pull down some tweets, and wrote a short loop to display them.

Here is the php I used to get the 10 latest tweets from my twitter account, “@wordofyourday”:

To finish, I included this php file in a basic web page that displayed the tweets, detailed my method, and listed the problems I ran into. You can see that page here. (I converted it to static html to avoid exposing my authentication keys.)


comments powered by Disqus