PHP script for getting latest Tweets of a user

There are so many different ways to get tweets to display on your blog or web page, unfortunately most of these use JavaScript which means that the tweets displayed are not made available to search engines 🙁

 

Don’t worry; you can also display your latest Twitter tweets using PHP. You can achieve by following very simple functions.

 

function getLastestTweets fetches latest tweets

It require 2 parameters
$userid = User Id of the twitter account
$x         = number of tweets to be fetched

[php]
<?php

function getLastestTweets($userid,$x){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=$x&quot";
$xml = simplexml_load_file($url) or die(‘could not connect’);
echo ‘<ul>’;
foreach($xml->status as $status){
$text = twitterify( $status->text );
echo ‘<li>’.utf8_decode($text).'</li>’;

$time = dateDiffForTweet(date("Y-m-d H:i:s"),date("Y-m-d H:i:s",strtotime($status->created_at)));
if(!sizeof($time)){ $time[0]= ‘1 second’; }
echo " ".$time[0]." ago";
}
echo ‘</ul>’;
}

?>
[/php]

function twitterify automatically converts hashtags (like #scriptarticle) to tag links, @links to author links (like @mahesh), and autolinks URIs (like http://scriptarticle.com ) to links.

[php]
<?php

function twitterify($ret) {
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" >\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $ret);
$ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $ret);
$ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" >#\\1</a>", $ret);
return $ret;
}

?>
[/php]

function dateDiffForTweet is used for showing time difference between tweet posted and current, like 2 mins ago,2 days ago

[php]
<?php

function dateDiffForTweet($time1, $time2, $precision = 6) {
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($time2)) {
$time2 = strtotime($time2);
}

// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;
}

// Set up intervals and diffs arrays
$intervals = array(‘year’,’month’,’day’,’hour’,’minute’,’second’);
$diffs = array();

// Loop thru all intervals
foreach ($intervals as $interval) {
// Set default diff to 0
$diffs[$interval] = 0;
// Create temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
$time1 = $ttime;
$diffs[$interval]++;
// Create new temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
}
}

$count = 0;
$times = array();
// Loop thru all diffs
foreach ($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
break;
}
// Add value and interval
// if value is bigger than 0
if ($value > 0) {
// Add s if value is not 1
if ($value != 1) {
$interval .= "s";
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
}
}

return $times;
// Return string with times
//return $times;
// return implode(", ", $times);
}

?>
[/php]

Here is the way; you can display your tweets

[php]
<?php

//my userid scriptarticle
getLastestTweets(‘scriptarticle’,5);

?>
[/php]

If you face any issue to implement the code just left a comments below the post.

2 thoughts on “PHP script for getting latest Tweets of a user”

  1. I’m new to this hence very confused. I need to extract the tweets of a particular user withing two dates. That is, and tweets posted between dates X and Y. How do I get those tweets in a PHP array?

Comments are closed.