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.

<?php
$twitterusername = "twitter-user-name";
$number = 15;
 
$feedurl = "http://search.twitter.com/search.json?q=from:" . $twitterusername . "&amp;rpp=" . $number;
 
$twitternewfile = dirname(__FILE__)."/twitternew.json";
$twitterfile = dirname(__FILE__)."/twittercurrent.json";
 
copy($feedurl, $twitternewfile);
 
$twitteroldcontent = @file_get_contents($twitterfile);
$twitternewcontent = @file_get_contents($twitternewfile);
 
if($twitteroldcontent != $twitternewcontent) {
copy($twitternewfile, $twitterfile);
}
$gettweets = @file_get_contents($twitterfile);
 
$gettweets = json_decode($gettweets);
 
echo "<ul>";
for($x=0;$x<$number;$x++) {
$str_feed = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $gettweets->results[$x]->text);
$pattern_feed = '/[#|@][^\s]*/';
preg_match_all($pattern_feed, $str_feed, $matches_feed);    
 
foreach($matches_feed[0] as $keyword_feed) {
$keyword_feed = str_replace(")","",$keyword_feed);
$link_feed = str_replace("#","%23",$keyword_feed);
$link_feed = str_replace("@","",$keyword_feed);
if(strstr($keyword_feed,"@")) {
$searchlink = "<a href=\"http://twitter.com/$link\">$keyword_feed</a>";
} else {
$searchlink = urlencode($link);
$nsearch = "<a href=\"http://twitter.com/#search?q=$searchlink\" class=\"grey\">$keyword_feed</a>";
}
$str_feed = str_replace($keyword_feed, $nsearch, $str_feed);
} 
echo "<li>".$str_feed."</li>\n";
}
echo "</ul>";
?>

Now we shall check on how the above code line works and performs the fetch operation. We first set the Twitter username with this code segment:

$twitterusername = "twitter-user-name";
$number = 15;

Now we pass this set values using the code line below to Twitter and we get a json feed back from the Twitter API. 

$feedurl = "http://search.twitter.com/search.json?q=from:" . $twitterusername . "&amp;rpp=" . $number;

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.

$twitternewfile = dirname(__FILE__)."/twitternew.json";
$twitterfile = dirname(__FILE__)."/twittercurrent.json";

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:

$twitteroldcontent = @file_get_contents($twitterfile);
$twitternewcontent = @file_get_contents($twitternewfile);

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:

if($twitteroldcontent != $twitternewcontent) {
copy($twitternewfile, $twitterfile);
}

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:


$gettweets = @file_get_contents($twitterfile); 
$gettweets = json_decode($gettweets);

Now we write the code segment of how the HTML display of the tweet can be done. This can be based on how the user wants the front-end design to visually appear to users:

echo "<ul>";
for($x=0;$x<$number;$x++) {
$str_feed = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $gettweets->results[$x]->text);
$pattern_feed = '/[#|@][^\s]*/';
preg_match_all($pattern_feed, $str_feed, $matches_feed);    
 
foreach($matches_feed[0] as $keyword_feed) {
$keyword_feed = str_replace(")","",$keyword_feed);
$link_feed = str_replace("#","%23",$keyword_feed);
$link_feed = str_replace("@","",$keyword_feed);
if(strstr($keyword_feed,"@")) {
$searchlink = "<a href=\"http://twitter.com/$link\">$keyword_feed</a>";
} else {
$searchlink = urlencode($link);
$nsearch = "<a href=\"http://twitter.com/#search?q=$searchlink\" class=\"grey\">$keyword_feed</a>";
}
$str_feed = str_replace($keyword_feed, $nsearch, $str_feed);
} 
echo "<li>".$str_feed."</li>\n";
}
echo "</ul>";

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.
 

Hire A Freelancer In Seconds

Looking to get some work done? Hire the world's best Designers, Developers, and Copywriters in just a few clicks.

Rajhees
Rajhees Hire Me

C.E.O, MD , Dn Infoway

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 - ...

Hire Me

Next Article

Web Design Trends to Forget