How to Build a Twitter Feed with PHP

Twitter is ranked among the top twenty most popular networking sites in the world, with millions of users logging on to the site every few seconds. Lots of applications have been built on the API provided by Twitter, and in this article you'll see how you can add a Twitter feed fetch and display operation using PHP. Place the following code snippet in a PHP file on the server and open the file in your browser. You will notice that your code has performed the extraction of 15 new feeds from the Twitter username you mentioned in your file.

During the first run of the above code file we create two json files under the current directory which will hold the previously extracted 15 feeds and the new extracted 15 feed for comparison and to update the site.

Now we copy the contents from the new extracted feed json file into the $twitternewfile using the code line:

copy($feedurl, $twitternewfile);

The contents of both these files are extracted into the variables $twitteroldcontent which will have the previously displayed twitter feed content and $twitternewcontent which will hold the new extracted content of twitter feeds using the following code segment:

We now compare the contents of both the files and check if they hold the same tweets displayed last in the site or they have new tweets. When they differ, we copy the new extracted feeds into the $twitterfile for future reference using the following code lines:

Now we get the $twitterfile content into the $gettweets variable to display the tweets on the front end and we decode the file contents into the same variable and keep it for reference using the following lines of code:

The above code segment processes the encoded twitterfeed json file using regular expression pattern and keyword matches and displays the feeds to the front-end based on simple unordered list HTML tags.

Subscribe Now For Weekly Updates

Stay up to date with the best articles on the Freelancer Community. Subscribe now for the best Design, Development, and Freelancing content each week.

DN Infoway is an offshore outsourcing company providing round-the-clock services to customers. We are a small team of ten people seven coders and three designers. We offer round the clock services in web design and development. Client satisfaction is the key to the success of DN Infoway.
Our key skills include:
- Wordpress
- Opencart
- Magento
- Drupal
- Joomla
- Parallax responsive websites
- ...