Get last tweet of your tweets or someone’s tweet with username doesn’t need OAuth. Twitter have their own API to get last tweet with username. You can access the API with http://api.twitter.com/1/statuses/user_timeline/, which some arguments supplied. You can read more detail about this API function here.
I have create a simple function to parse last tweet from a username:
function parse_feed($usernames, $limit=5) { $usernames = str_replace("www.", "", $usernames); $usernames = str_replace("http://twitter.com/", "", $usernames); $username_for_feed = str_replace(" ", "+OR+from%3A", $usernames); $feed = "http://api.twitter.com/1/statuses/user_timeline/".$username_for_feed.".atom?callback=?"; $cache_rss = file_get_contents($feed); if (!$cache_rss) { // we didn't get anything back from twitter echo "<!-- ERROR: Twitter feed was blank! Using cache file. -->"; } // clean up and output the twitter feed $feed = str_replace("&", "&", $cache_rss); $feed = str_replace("<", "<", $feed); $feed = str_replace(">", ">", $feed); $clean = explode("<entry>", $feed); $clean = str_replace(""", "'", $clean); $clean = str_replace("'", "'", $clean); $amount = count($clean) - 1; if($amount > $limit) $amount=$limit; $tweets = array(); if ($amount) { // are there any tweets? for ($i = 1; $i <= $amount; $i++) { $entry_close = explode("</entry>", $clean[$i]); $clean_content_1 = explode("<content type="html">", $entry_close[0]); $clean_content = explode("</content>", $clean_content_1[1]); $clean_name_2 = explode("<name>", $entry_close[0]); $clean_name_1 = explode("(", $clean_name_2[1]); $clean_name = explode(")</name>", $clean_name_1[1]); $clean_user = explode(" (", $clean_name_2[1]); $clean_lower_user = strtolower($clean_user[0]); $clean_uri_1 = explode("<uri>", $entry_close[0]); $clean_uri = explode("</uri>", $clean_uri_1[1]); $clean_time_1 = explode("<published>", $entry_close[0]); $clean_time = explode("</published>", $clean_time_1[1]); $unix_time = strtotime($clean_time[0]); $pretty_time = relativeTime($unix_time); $tweets[] = array('content' => $clean_content[0], 'time' => $pretty_time); } } return $tweets; }
How to use it:
print_r(parse_feed('ivankrisdotcom'));
The function will return array of tweets. That’s it, if you like to know more, you can follow me on Twitter or Facebook.
This is a pretty hacky method, that could result in a lot of issues, particularly with character encodings.
Just load the atom feed with and then do an xpath query on it to get all the data you want on the last 20 tweets.
$xml = simplexml_load_file($url);
foreach ($xml->xpath('entry') as $entry) {
print $entry->title; //prints the tweet
}
Or better yet load the json file version and decode with json_decode to get a nice object).
Thank you very much for the tips mate. I'll try it soon.