Link to home
Start Free TrialLog in
Avatar of maccaj51
maccaj51Flag for Afghanistan

asked on

Php Get Latest Tweets

Hi Experts,

I need a script that outputs my last two tweets , with the date so I can format it correctly?

It also seems like any script I find wont publish tweets older than about 24/48hrs...

Could someone assist me on this

Many thanks in advance
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

As per this documentation, it may not be possible. Check out this
check out, " count" parameter,


"count: Specifies the number of tweets to try and retrieve, up to a maximum of 200."

Open in new window

Avatar of maccaj51

ASKER

Hi  logudotcom

Many thanks for your help...

Could you just suggest a basic script based on this?

Im a bit of a beginner
I had worked earlier to get the latest tweets and processed for some web service. Never used the time frame to fetch the tweets .. I always to get the latest tweets... So, it is better you work with as per the documentation help.

This also help for you, https://dev.twitter.com/docs/working-with-timelines
Just to clarify, let me try to paraphrase.  You will Tweet, perhaps using your cell phone or iPad, and you want an application that can get your last two tweets whenever the application is run.  Is that a good summary?
Hi Ray,

I need a php script that will display the last two tweets from my account on my website... displaying the time they were tweeted as well

Thanks
try this script, it should display latest 2 tweets

 <?php
/**
	
/**
* Method to tweets 
*  
*/
function fetch_rss_tweets($username, $maxtweets) {
     //Using simplexml to load URL
     $tweets = simplexml_load_file("http://twitter.com/statuses/user_timeline/" . $username . ".rss");
		
		
     $tweet_array = array();  //Initialize empty array to store tweets
     foreach ( $tweets->channel->item as $tweet ) { 
	 
	 	/*	print '<pre>';
			print_r ($tweet);
			print '</pre>';*/

          //Loop to limitate nr of tweets.
          if ($maxtweets == 0) {
               break;
          } else {
               $twit = $tweet->description;  //Fetch the tweet itself

               //Remove the preceding 'username: '
               $twit = substr(strstr($twit, ': '), 2, strlen($twit));

               // Convert URLs into hyperlinks
             //  $twit = preg_replace("/(http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*)/", "<a href=\"\\0\">\\0</a>", $twit);

               // Convert usernames (@) into links 
               $twit = preg_replace("(@([a-zA-Z0-9\_]+))", "<a href=\"http://www.twitter.com/\\1\">\\0</a>", $twit);

               // Convert hash tags (#) to links 
               $twit = preg_replace('/(^|\s)#(\w+)/', '\1<a href="http://search.twitter.com/search?q=%23\2">#\2</a>', $twit);

               //Specifically for non-English tweets, converts UTF-8 into ISO-8859-1
               $twit = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $twit);

               
			   //Store tweet and time into the array
               $tweet_item = array(
                     'desc' => $twit,                    					 
               );
               array_push($tweet_array, $tweet_item);

               $maxtweets--;
          }
     }
     //Return array
     return $tweet_array;
}

?>


<?php



// Run the function, with your twitter username and number of tweets as arguments
$mtweets = fetch_rss_tweets('244827095', 2);

echo '<ul class="twitter-updates">';
foreach ($mtweets as $k => $v) {
    echo '<li><p><span class="update">' .$v['desc']. '</span></p>';	
	$urls = explode (" ", $v['desc']);
	
	echo "<br/>";
}
echo '</ul>';



?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many Thanks for all your help... have decided with this script
Doing some further testing, the count=3 bug is not always present.  So I think I might want to go with three but limit the display to two.  The tweets appear to come back in reverse-chronological order, however if you found a disparity in the sequence you might be able to make an array and sort them by date.