26 Jan

How to create a Twitter web client using PHP

In this tutorial you will learn how to create a twitter web client using php. The web client we are going to build will be able to load tweets with a given hashtag and that has been retweeted at least once.

To start with you need to register a Twitter application with read only access. Read this blog post: How to register a Twitter app

Get Source Code on Github


Once you have grabbed the source code, open the Feed Class in includes folder.
twitter web client

Twitter Feed Class (includes/feed.php)

This file contains the app logic for authenticating user to reading tweets.
check_connection() function checks if user is cnnected via twitter or not. If yes, then user is shown twitter feed else user is redirected to page where he/she is asked to sign in to twitter.

callback(): This function handles the callback from twitter when user is redirected back to our website after authorizing our app.

get_tweets_by_hashtag($hashtag): This function is responsible for fetching tweets from twitter of a given hashtag. Note that while searching i have added ‘-RT’ to our search string, this will help in omitting those tweets which are retweets, since we only want to show original tweets and not their copies.

 

Script.js (js/script.js)

To make the client ajax based i have written three ajax functions to load tweets i.e one for initial page load, to load older tweets and on to load new tweets.

 

Besides this, I have created a view file view/tweets_feed_vew.php which shows all the tweets in browser. The file also contains logic to show tweets which have retweet count of more than 1.
Rest of the code is self explanatory.

That’s it, download the code and refer read me file to run the code on your  machine. If you have any doubts/questions please feel free to ask me in comments.

The following two tabs change content below.

Vineet Garg

Sr. Web Developer at GuestHouser
I am passionate about technology and web development.

Latest posts by Vineet Garg (see all)